Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Today i am facing problem to fill datagridview1. i have created void for fill the gridview from data source. it is working fine in buttons click but not fill if i putted it to the page load.

What I have tried:

C#
private void fillgidview()
        {
            try
            {
                cnn.Open();
                string sqlcommand = " select OPD_NO as [OPD No],Patientname as [Pateint Name],Age,VisitDate as [visit Date],Department,Consultent_Doctor  from OPD_table  where Status_pet='Checked In'   and Consultent_Doctor='" + label5.Text + "'";

                SqlDataAdapter adap = new SqlDataAdapter(sqlcommand, cnn);

                DataTable dt = new DataTable();

                adap.Fill(dt);
                
                this.dataGridView1.DataSource = dt;
                
                cnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());

            }
        }
Posted
v2
Comments
Sinisa Hajnal 7-Oct-16 4:15am    
IF this is web development (as implied by Page load) then you need to call dataGridView1.DataBind()

If it is not (as implied by use of DataGridView) then you need to do form load not page load event.

Also, you should never EVER add user input value to your SQL.
Member 10532715 7-Oct-16 4:37am    
yes it is windows desktop application pls see my full code

previous page code where i have putted

private void adminMenuToolStripMenuItem_Click(object sender, EventArgs e)
{
FrmDoctorOPD doctoropd = new FrmDoctorOPD();
doctoropd.Show();
doctoropd.label5.Text = label12.Text;



}

now in page to i have putted it as..

private void FrmDoctorOPD_Load(object sender, EventArgs e)
{

label7.Text = System.DateTime.Now.ToShortDateString();
fillgidview();


}

private void fillgidview()
{
try
{
cnn.Open();
string sqlcommand = " select OPD_NO as [OPD No],Patientname as [Pateint Name],Age,VisitDate as [visit Date],Department,Consultent_Doctor from OPD_table where Status_pet='Checked In' and Consultent_Doctor='" + label5.Text + "'";


SqlDataAdapter adap = new SqlDataAdapter(sqlcommand, cnn);


DataTable dt = new DataTable();

adap.Fill(dt);

this.dataGridView1.DataSource = dt;


cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());

}




}

1 solution

Refer - How to: Bind Data to the Windows Forms DataGridView Control[^].

Make sure your data is coming from database.
 
Share this answer
 
Comments
Member 10532715 7-Oct-16 4:41am    
yes it is coming from data base.. when i use search button .. its working fine but not in page load.. pls see search button code it is working like charm..

private void search()
{

try
{
cnn.Open();
string sqlcommand = " select OPD_NO as [OPD No],Patientname as [Pateint Name],Age,VisitDate as [visit Date],Department,Consultent_Doctor from OPD_table where Status_pet='Checked In' and Consultent_Doctor='" + label5.Text + "' and visitdate between '" + DateTime.ParseExact(this.dateTimePicker1.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture) + "' and '" + DateTime.ParseExact(this.dateTimePicker2.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture) + "'";


SqlDataAdapter adap = new SqlDataAdapter(sqlcommand, cnn);


DataTable dt = new DataTable();

adap.Fill(dt);

if (dt.Rows.Count > 0)
{

this.dataGridView1.DataSource = dt;

cnn.Close();
}

else
{
MessageBox.Show("No Records found");
dataGridView1.DataSource = null;
cnn.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());

}



}
Your query is different here inside the search method. Can you debug and see in the code if the data is coming and binding to the datatable?
Member 10532715 7-Oct-16 4:42am    
private void metroTile3_Click(object sender, EventArgs e)
{
search();

}
Member 10532715 7-Oct-16 6:54am    
It has been solved, actually my constructor pass data after form load i have putted it to after.. then its working fine.. Thanks Mr.Tadit.. for looking my problem...

wrong code..
FrmDoctorOPD doctoropd = new FrmDoctorOPD();
doctoropd.Show();
doctoropd.label5.Text = label12.Text; /// here is wrong afterload

Correct code..

FrmDoctorOPD doctoropd = new FrmDoctorOPD();
doctoropd.label5.Text = label12.Text; before load
doctoropd.Show();
Glad to hear that. Most welcome. Keep coding. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900