Frage

I have a datagridview in my Windows C# application. I am binding some data from my SQL server 2005 database into it. The datagridview already has a fixed number of columns and rows with their indivdual names. The problem is the data is displayed after the columns where i wish to get my data. The code is as following

SqlCommand cmd = new SqlCommand("Select * from INV_details_1 where i_n = '" + textBox3.Text + "'", sconn);

            SqlDataAdapter da2 = new SqlDataAdapter();

            da2.SelectCommand = cmd;

            DataSet ds2 = new DataSet();

           da2.Fill(ds2);

            dgv_details.DataSource = ds2.Tables[0].DefaultView;

The issue is that the data is getting displayed after my desired column headers. I wish to get the data in these specified columns.

Please help

War es hilfreich?

Lösung

You must map each column of the datagridview with the corresponding column in database using DataPropertyName property of Datagridview's column. EX:

dgv_details.Columns[0].DataPropertyName = "Name"

This will map column 0 in datagridview with column "Name" in datatable

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top