我的datagridview没有显示数据

时间:2012-02-16 16:44:46

标签: c# sql datagridview dataset

我创建了一个连接到数据集的datagridview。数据集本身有很多记录,我已经检查过;但是当我调试时,网格没有显示ANYTHING,这很令人恼火,因为就在几天前,我的这个程序工作得很好。我没有修改任何代码或文件。我刚刚打开我的项目几天就休息了一会儿,当我再次打开它的时候,它还没有像以前一样好用。这是我使用的代码:

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!");
}

我很确定它与我的关系有关。任何帮助都会很棒!

2 个答案:

答案 0 :(得分:0)

1 - 您是否尝试过集成安全性= SSPI?

2 - MaxRows = custMaster.Tables [“custMaster”]。Rows.Count;试着看看你的数据表返回了多少行?

3 - 您是否有错误或只是空数据网格?你试过运行调试器吗?

答案 1 :(得分:0)

我会评论,但我的代表不够高......无论如何,数据集仍然有数据,而datagridview几天前显示的数据,但现在不是?如果是这种情况,我不知道为什么它会在几天前工作,现在却不是,但也许你可以在加载数据集后尝试刷新datagridview。