Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datagridview doesn't display data

Tags:

c#

i want to show the data from the database onto my datagridview control i have used the following piece of code but it is not showing any data when the form loads it just shows and empty datagridview i don't get any errors what am I doing wrong

    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.AutoGenerateColumns = false;
        FillData();

    }
    public void FillData()
    {
        using (SqlConnection myConnection = new SqlConnection("server=localhost;" +
                                   "Trusted_Connection=yes;" +
                                   "database=database; " +
                                   "connection timeout=10"))
        {
            myConnection.Open();

            using (SqlDataAdapter sqlDa = new SqlDataAdapter("select * from スコープ", myConnection))
            {
                DataTable dt = new DataTable();
                sqlDa.Fill(dt);
                dataGridView1.DataSource = dt;

            }

        }
    }
like image 796
Pradeep Singh Avatar asked Dec 22 '25 00:12

Pradeep Singh


1 Answers

I suspect there is no matching column of datatable with datagridview column....Check the column of datagridview with datatable column....

For test, make dataGridView1.AutoGenerateColumns to true and check whether the datagridview fill the data or not...

dataGridView1.AutoGenerateColumns= true;

You can create the datagridview column by following way:

Go to properties of datagridview and then go to Columns Section where you can add new column in datagridview according to your datatable....Match DataPropertyName with your datatable column and keep AutoGenerateColumns to false and then it'll works fine...

like image 165
Akash KC Avatar answered Dec 23 '25 13:12

Akash KC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!