ADO.net datagridview绑定数据源后未显示数据??

朔北冥 2016-05-02 08:49:43
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace phoneBook1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}
public DataSet ds;
public DataRow[] dr;
public int i = 0;
public void update()
{
dgv.DataSource = ds.Tables[0];
}

private void Form1_Load(object sender, EventArgs e)
{
enter form = new enter();
form.ShowDialog();
if (enter.flag == 0)
{
this.Close();
}
dgv.DataSource = bandsource().Tables[0];
dgv.Visible = false;
}
}
...全文
260 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
朔北冥 2016-05-03
  • 打赏
  • 举报
回复
还有一个问题,我同代码将dataset保存至数据库中,但是我打开数据库(.mdb格式的)却还是原来的数据,这是怎么回事? 代码:

 public Form1()
        {
            InitializeComponent();
           
        }
        public static DataSet ds;
        public static DataRow[] dr;
        public static OleDbDataAdapter da;
        public static int i = 0;
        public static int n = 1;


        //更新数据
        public void updategvd()
        {

            dgv.DataSource = ds.Tables[0];
        }


        //获得textbox中内容


        public string[] textGet()
        {
            string[] str=new string[6];
            str[0] = Name.Text.ToString(); str[1] = Sex.Text.ToString(); str[2] = HomeTel.Text.ToString();
            str[3] = OfficeTel.Text.ToString(); str[4] = Mark.Text.ToString();
            return str;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO:  这行代码将数据加载到表“telephoneDataSet.telephoneinfo”中。您可以根据需要移动或删除它。
            this.telephoneinfoTableAdapter.Fill(this.telephoneDataSet.telephoneinfo);//自动添加??
            enter form = new enter();
            form.ShowDialog();
            if (enter.flag == 0)
            {
                this.Close();
            }
            dgv.DataSource = bandsource().Tables["phone"];
        }
        public DataSet bandsource()
        {
            string str = "provider=Microsoft.Jet.OLEDB.4.0; data Source=telephone.mdb; Persist Security Info=False";
            OleDbConnection conn = new OleDbConnection(str);
            ds = new DataSet();
            try
            {
                conn.Open();
                string sqlstr = "select telephoneinfo.PersonID as 自动编号,telephoneinfo.Name as 姓名,telephoneinfo.Sex as 性别,telephoneinfo.OfficeTel as 办公室电话,telephoneinfo.HomeTel as 家庭电话,telephoneinfo.Mark as 备注 from telephoneinfo order by PersonID";
                da = new OleDbDataAdapter(sqlstr, conn);
                da.Fill(ds, "phone");
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                conn.Close();
            }
            return ds;
        }
 private void save_Click(object sender, EventArgs e)
        {
            OleDbCommandBuilder builder = new OleDbCommandBuilder(da);
            da.Update(ds, "phone");
            ds.AcceptChanges();
        }

        private void updateData_Click(object sender, EventArgs e)
        {
            ds.Clear();
            da.Fill(ds, "phone");
            dgv.DataSource = ds.Tables["phone"]; 
        }
其中我通过update来更新数据dataset是显示的是修改之后的结果,但是当我打开数据库时,发现数据并没有更新。
朔北冥 2016-05-03
  • 打赏
  • 举报
回复
谢谢二楼与三楼,我改了下代码的dataset变量将其改成static就行了。不够我是通过函数返回dataset,在通过dataset的tables来绑定datagriview的数据源。 莫非是因为函数返回的是局部变量无法绑定,很是不理解。
朔北冥 2016-05-03
  • 打赏
  • 举报
回复
谢谢二楼与三楼,我改了下代码的dataset变量将其改成static就行了。不够我是同函数返回dataset,在通过dataset的tables来绑定。 莫非是因为函数返回的是局部变量无法绑定,很是不理解。
public Form1()
        {
            InitializeComponent();
           
        }
        public static DataSet ds;
        public static DataRow[] dr;
        public static OleDbDataAdapter da;
        public static int i = 0;
        public static int n = 1;


        //更新数据
        public void updategvd()
        {

            dgv.DataSource = ds.Tables[0];
        }


        //获得textbox中内容


        public string[] textGet()
        {
            string[] str=new string[6];
            str[0] = Name.Text.ToString(); str[1] = Sex.Text.ToString(); str[2] = HomeTel.Text.ToString();
            str[3] = OfficeTel.Text.ToString(); str[4] = Mark.Text.ToString();
            return str;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO:  这行代码将数据加载到表“telephoneDataSet.telephoneinfo”中。您可以根据需要移动或删除它。
            this.telephoneinfoTableAdapter.Fill(this.telephoneDataSet.telephoneinfo);//自动添加??
            enter form = new enter();
            form.ShowDialog();
            if (enter.flag == 0)
            {
                this.Close();
            }
            dgv.DataSource = bandsource().Tables["phone"];
        }
        public DataSet bandsource()
        {
            string str = "provider=Microsoft.Jet.OLEDB.4.0; data Source=telephone.mdb; Persist Security Info=False";
            OleDbConnection conn = new OleDbConnection(str);
            ds = new DataSet();
            try
            {
                conn.Open();
                string sqlstr = "select telephoneinfo.PersonID as 自动编号,telephoneinfo.Name as 姓名,telephoneinfo.Sex as 性别,telephoneinfo.OfficeTel as 办公室电话,telephoneinfo.HomeTel as 家庭电话,telephoneinfo.Mark as 备注 from telephoneinfo order by PersonID";
                da = new OleDbDataAdapter(sqlstr, conn);
                da.Fill(ds, "phone");
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                conn.Close();
            }
            return ds;
        }
全栈极简 2016-05-03
  • 打赏
  • 举报
回复
dgv.Visible = false; //true
bdmh 2016-05-02
  • 打赏
  • 举报
回复
第一检查是envy有数据返回,然后看看表格是否添加了列,是否绑定了对应的字段
朔北冥 2016-05-02
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace phoneBook1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
        }
        public DataSet ds;
        public DataRow[] dr;
        public  int i = 0;
        public void update()
        {
            dgv.DataSource = ds.Tables[0];
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            enter form = new enter();
            form.ShowDialog();
            if (enter.flag == 0)
            {
                this.Close();
            }
            dgv.DataSource = bandsource().Tables[0];
            dgv.Visible = false;
        }
        public DataSet bandsource()
        {
            string str = "provider=Microsoft.Jet.OLEDB.4.0; data Source=telephone.mdb; Persist Security Info=False";
            OleDbConnection conn = new OleDbConnection(str);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
                string sqlstr = "select PersonID ,Name,Sex,OfficeTel,HomeTel,Mark from telephoneinfo order by PersonID";
                OleDbDataAdapter da = new OleDbDataAdapter(sqlstr, conn);
                da.Fill(ds, "phone");
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                conn.Close();
            }
            return ds;
        }
}

8,833

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 组件/控件开发
社区管理员
  • 组件/控件开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧