开发者

how to set index of each column manually in datagridview after binding data in winforms?

code to add dynamic data

getdata()  
{  
SqlConnection con = new SqlConnection("Data source=XXXXX;Initial catalog=dummy;integr开发者_StackOverflow中文版ated 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.


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.


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...

how to set index of each column manually in datagridview after binding data in winforms?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜