SQL data in DataGridView

后端 未结 2 2003
猫巷女王i
猫巷女王i 2021-01-29 01:47

Hey guys, I have an application that I want to display some data from a sql db in a DataGridView... I have the data displayed in the DataGridView now but here are my questions..

相关标签:
2条回答
  • 2021-01-29 02:23

    For hiding you columns ("Row_ID") You can use

    dgView.Columns[0].Visible = false;
    

    For setting header text

    dgView.Columns[1].HeaderText = "PartNumber";
    
    0 讨论(0)
  • 2021-01-29 02:25

    If you want to use Custom titles etc. First of all create a new datatable, and give the names. Be Careful about typeof()

    DataTable dt = new DataTable();
    
            dt.Columns.Add("Title 1",typeof(int));
            dt.Columns.Add("Title 2", typeof(string));
            dt.Columns.Add("Title 3", typeof(DateTime));
            dt.Columns.Add("Title 4", typeof(bool));
            dt.AcceptChanges();
    

    Get all data which comes from SQL and create object for them in

    foreach(var item in yourSqlData)

    object[] row = new object[] 
            { 
            item.data1.ToString(), // string columns
            Convert.ToInt32(item.data2), // int columns
            Convert.ToDateTime(item.data3), // datetime column
            Convert.ToBoolean(item.data4) // bool column
            };
            dt.Rows.Add(row);
    

    and bind dt to your gridView

    0 讨论(0)
提交回复
热议问题