Custom column names for DataGridView with associated DataSource

前端 未结 3 676
走了就别回头了
走了就别回头了 2020-12-05 13:28

How can I setup custom column names for DataGridView with associated DataSource?

Here is some code:

class Key
{
    public string Value { get; }
             


        
3条回答
  •  北海茫月
    2020-12-05 14:22

    public DataTable SetColumnsHeaderName(string[] TagName)
    {
        DataTable dt = new DataTable();
        foreach (var item in TagName)
        {
            dt.Columns.Add(item);
        }
        return dt;
    }
    
    public void ShowDataGridView()
    {
        string[] tag = new string[] { "First Name", "Last Name", "Phone Number", };
        //fill datatable columns Name
        var dt = SetColumnsHeaderName(tag);
        //connect db and fetch table name
        var listx = db.tablename.ToList();
        foreach (var item in listx)
        {
            dt.Rows.Add(new object[] { item.Name, item.Family, item.Phone });
        }
        datagridView.DataSource = dt;
    }
    

提交回复
热议问题