我的数据网格视图没有显示数据
本文关键字:数据 显示 网格 数据网 我的 视图 | 更新日期: 2023-09-27 18:24:47
我制作了一个连接到数据集的数据网格视图。数据集本身有很多记录,我已经检查过了;但当我调试时,网格没有显示任何东西,这很令人恼火,因为就在几天前,我的这个程序还运行得很好。我没有为此修改任何代码或文件。我刚从打开我的项目中休息了几天,当我再次打开它时,砰的一声,它没有以前那么好用了。这是我使用的代码:
System.Data.SqlClient.SqlConnection con; //sweet connection object is created in this here line
masterDataSet custMaster; //this creates an AWESOME database object
System.Data.SqlClient.SqlDataAdapter da; //this sets up a data adapter named "da". kewl
int MaxRows = 0;
int inc = 0;
private void Form1_Load(object sender, EventArgs e)
{
con = new System.Data.SqlClient.SqlConnection(); //name of the sweet connection object above
custMaster = new masterDataSet();
con.ConnectionString = "Data Source=.''SQLEXPRESS;AttachDbFilename=|DataDirectory|''master.mdf;Integrated Security=True;User Instance=True"; //this tells windows where to find the database DORA STYLE
con.Open(); //this opens up the connection. I DON'T SAY?
MessageBox.Show("Database connection has been established succesfully.");
string sql = "SELECT * From custMaster";
da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da.Fill(custMaster, "custMaster");
MaxRows = custMaster.Tables["custMaster"].Rows.Count;
da.Update(custMaster, "custMaster");
con.Close();
}
然后,为了输入数据,我用下面的代码创建了一个表单。尽管我确信错误不在这一部分,但我还是会输入:
private void newCustbutton1_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlCommandBuilder cb;
cb = new System.Data.SqlClient.SqlCommandBuilder(da);
DataRow dRow = custMaster.Tables["custMaster"].NewRow();
dRow[1] = a_newCust.Text;
dRow[2] = b_newCust.Text;
dRow[3] = c_newCust.Text;
dRow[4] = d_newCust.Text;
dRow[5] = e_newCust.Text.ToString();
dRow[6] = f_newCust.Text;
dRow[7] = g_newCust.Text.ToString();
if (radioButton1.Checked == true)
{
dRow[8] = radioButton1.Text;
}
else if (radioButton2.Checked == true)
{
dRow[8] = radioButton2.Text;
}
custMaster.Tables["custMaster"].Rows.Add(dRow);
MaxRows = MaxRows + 1;
inc = MaxRows - 1;
da.Update(custMaster, "custMaster");
MessageBox.Show("Customer succesfully added!");
}
我确信这与我的关系有关。任何帮助都会很棒!
1-您是否尝试过将集成安全设置为SSPI?
2-MaxRows=custMaster.Tables["custMaster"].Rows.Count;试着看看你的数据表返回了多少行?
3-是否存在您正在获取的错误或只是清空数据网格?你试过运行调试器了吗?
我想发表评论,但我的代表还不够高。。。不管怎样,数据集仍然有数据,数据网格视图几天前显示了数据,但现在没有了?如果是这样的话,我不知道为什么几天前它会起作用,现在却不起作用,但也许你可以在加载数据集后尝试刷新datagridview。