how to set index of each column manually in datagridview after binding data in winforms?
Solution 1
From a Form Designer add new DataGridViewTextBoxColumn
and move it to top most position or at whatever position you would like to display this column at, then in properties of this DataGridViewTextBoxColumn
set DataPropertyName
to the name of your column, which from your code, i can see that is Title, that's it, compile, run and it should display the text column before button columns... I have attached a screenshot for you, I hope it will solve your problem...
Solution 2
To change a column's position in a DataGridView set the DataGridViewColumn's DisplayIndex property to the desired value:
dataGridView1.Columns[2].DisplayIndex = 0;
This code changes the third DGV column to the first column position.
Note you don't need to change the columns in your DataTable and changing the order of the columns in a DataGridView will not affect the schema of your DataTable.
tiru
Updated on April 16, 2020Comments
-
tiru about 4 years
code to add dynamic data
getdata() { SqlConnection con = new SqlConnection("Data source=XXXXX;Initial catalog=dummy;integrated security=true"); DataTable dt = new DataTable(); DataColumn col = new DataColumn("Title", typeof(System.String)); dt.Columns.Add(col); DataRow row = dt.NewRow(); con.Open(); SqlCommand cmd = new SqlCommand("select Title,Description from systemtray", con); SqlDataReader dr; dr = cmd.ExecuteReader(); while (dr.Read()) { string s=dr["Title"].ToString()+Environment.NewLine+dr["Description"].ToString(); row["Title"]= s.Trim(); dt.Rows.Add(row["Title"]); } dataGridView1.DataSource = dt; }
and i add two buttons to datagrid using properties.Here the out put is:two buttons are displaying at index [0],[1] and the data is displaying at last column,i need to display the data at index[0] position then two buttons.how can i set index manually.
-
tiru almost 13 yearsThank you for your response sir.Yes,sir it is working well now.Thanks for the help.