简体   繁体   中英

My datagridview is not displaying data

I made a datagridview which was connected to a dataset. The dataset itself has plenty of records, I've checked; but when I debug, the grid doesn't display ANYTHING, which is irritating because just a couple of days ago, this program of mine was working just fine. I haven't modified any code or file for that matter. I just took a break from opening my project for a couple of days, and when I opened it again, boom, it's not working as well as before. Here's the code that I use:

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();
}

then, for inputting data, I have created a form with the following code. although I'm sure the error does not lie in this part, I'll type it in anyway:

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

I'm pretty sure it has do with my connections. Any help would be great!

1 - Have you tried to put Integrated Security=SSPI?

2 - MaxRows = custMaster.Tables["custMaster"].Rows.Count; try to see how many rows ur datatable is returning?

3 - is there an error that you are getting or just empty datagrid? have you tried to run the debugger?

I would comment but my rep's not high enough... Anyway, so the dataset still has data and the datagridview was displaying the data a few days ago but now it isn't? If this is the case, I don't know why it would be working a few days ago and now it isn't, but maybe you could try refreshing the datagridview after you load the dataset.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM