无法在桌面应用程序的代码中绑定网格视图 [英] Not able to bind the grid view in code behind desktop application

查看:86
本文介绍了无法在桌面应用程序的代码中绑定网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格视图,列出所有的客户。



我在Form的加载时间中绑定它,该格式放在MDI的孩子中。 p>

网格视图中的列是在设计时预定义的。



我的代码 Form_Load()事件是:

  try 
{
cn = db .createConnection();
if(cn.State == System.Data.ConnectionState.Open)
{
cn.Close();
}
cn.Open();
cmd = new OleDbCommand(选择BillNo,PartyName,City,State,FORMAT(BillDt,dd-mm-yyyy))作为BillMaster的BillDt,cn);
da = new OleDbDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
cn.Close();

dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = ds.Tables [0];
for(int i = 0; i< ds.Tables [0] .Rows.Count; i ++)
{
dataGridView1.Rows [i] .Cells [0] .Value = ds.Tables [0] .Rows [i] [BillNo]。ToString();
dataGridView1.Rows [i] .Cells [1] .Value = ds.Tables [0] .Rows [i] [PartyName]。ToString();
dataGridView1.Rows [i] .Cells [2] .Value = ds.Tables [0] .Rows [i] [City]。ToString();
dataGridView1.Rows [i] .Cells [3] .Value = ds.Tables [0] .Rows [i] [State]。ToString();
dataGridView1.Rows [i] .Cells [4] .Value = ds.Tables [0] .Rows [i] [BillDt]。ToString();
}

}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
cn.Close();
da.Dispose();
ds.Dispose();
cmd.Dispose();
}

}

我通过放置来检查程序执行断点。数据完全与DataSet和Immediate Window中的数据库一起获取,Grid的特定单元格的值显示确切的值,但问题是当窗体加载时,网格保持为空。并创建与从数据库中提取的行数相同的空白行数。



我应该怎么处理这个错误。



请帮助。

解决方案

  try 
{
cn = db.createConnection();
if(cn.State == System.Data.ConnectionState.Open)
{
cn.Close();
}
cn.Open();
cmd = new OleDbCommand(选择BillNo,PartyName,City,State,FORMAT(BillDt,dd-mm-yyyy))作为BillMaster的BillDt,cn);
da = new OleDbDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
cn.Close();

dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = ds.Tables [0];

//或者你可以使用
//dataGridView1.DataSource = ds.Tables [0] .DefaultView;


}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
cn.Close();
da.Dispose();
ds.Dispose();
cmd.Dispose();
}

}

希望它帮助你


I have a grid view which lists all the customers.

I am binding it in the load time of Form which is placed in the child of MDI.

Columns in the grid view is predefined at the design time.

My code for the Form_Load() event is:

try
{
       cn = db.createConnection();
       if (cn.State == System.Data.ConnectionState.Open)
       {
           cn.Close();
       }
       cn.Open();
       cmd = new OleDbCommand("Select BillNo,PartyName,City,State,FORMAT(BillDt,'dd-mm-yyyy') as BillDt from BillMaster", cn);
       da = new OleDbDataAdapter(cmd);
       ds = new DataSet();
       da.Fill(ds);
       cn.Close();

       dataGridView1.AutoGenerateColumns = false;
       dataGridView1.DataSource = ds.Tables[0];
       for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
       {
            dataGridView1.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i]["BillNo"].ToString();
            dataGridView1.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i]["PartyName"].ToString();
            dataGridView1.Rows[i].Cells[2].Value = ds.Tables[0].Rows[i]["City"].ToString();
            dataGridView1.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i]["State"].ToString();
            dataGridView1.Rows[i].Cells[4].Value = ds.Tables[0].Rows[i]["BillDt"].ToString();
        }

   }
   catch (Exception ex)
   {
        MessageBox.Show(ex.Message.ToString());
   }
   finally
   {
       cn.Close();
       da.Dispose();
       ds.Dispose();
       cmd.Dispose();
   }

}

I checked the program execution by putting the breakpoints. The data is fetched exactly as database in the DataSet and Immediate Window the value of particular cell of grid shows the exact value but the problem is when the form is loaded the grid remains empty. And creates the number of blank rows same as the number of rows fetched from the database.

What should I do to tackle this error.

Please help.

解决方案

try
{
   cn = db.createConnection();
   if (cn.State == System.Data.ConnectionState.Open)
   {
       cn.Close();
   }
   cn.Open();
   cmd = new OleDbCommand("Select BillNo,PartyName,City,State,FORMAT(BillDt,'dd-mm-yyyy') as BillDt from BillMaster", cn);
   da = new OleDbDataAdapter(cmd);
   ds = new DataSet();
   da.Fill(ds);
   cn.Close();

   dataGridView1.AutoGenerateColumns = true;
   dataGridView1.DataSource = ds.Tables[0];

   //Or you can use
   //dataGridView1.DataSource = ds.Tables[0].DefaultView;    


   }
   catch (Exception ex)
   {
    MessageBox.Show(ex.Message.ToString());
   }
   finally
   {
   cn.Close();
   da.Dispose();
   ds.Dispose();
   cmd.Dispose();
  }

  }

Hope it helps you

这篇关于无法在桌面应用程序的代码中绑定网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆