开发者

How to get selected columns from a datatable

I have a datatable with 10 columns, but User can select the columns to be displayed so I need to create a generic code to display only the selected columns.

Current code:
    string[] selectedColumns = new[] { };
    DataTable columns = new Da开发者_如何学GotaView(table).ToTable(false, selectedColumns); 


You could simply hide the columns in the datagridview (supposing you're using one)

dataGridView1.Columns["ColName"].Visible = False;

for every column that are not in selectedColumns

But if you really need to filter the dataset, I remember that you can do something like:

mydatatable.Columns.Remove("ColName");

in your datatable... or in a copy.

If you are going to show the columns in a datagridview, my option will be setting the ColumnMapping property of the datatable column like this

mydatatable.Columns["Colname"].ColumnMapping = MappingType.Hidden;

Unfortunatly I'm not at home now, so I can't test it, but as you can see, there are many options.

EDIT: In response to your request, you can deal with non selected columns like this:

    for(int i = 0;i<dt.Columns.Count;i++)
    {
        if(!selectedColumns.Contains(dt.Columns[i].ColumnName))
        {
            dt.Columns[i].ColumnMapping = MappingType.Hidden;
        }
    }


   public static void Main(string[] args)
    {
        DataTable act = new DataTable();
        act.Columns.Add(new DataColumn("id", typeof(System.Int32)));
        act.Columns.Add(new DataColumn("name"));
        act.Columns.Add(new DataColumn("email"));
        act.Columns.Add(new DataColumn("phone"));

        DataRow dr = act.NewRow();
        dr["id"] = 101;
        dr["name"] = "Rama";
        dr["email"] = "rama@mail.com";
        dr["phone"] = "0000000001";
        act.Rows.Add(dr);

        dr = act.NewRow();
        dr["id"] = 102;
        dr["name"] = "Talla";
        dr["email"] = "talla@mail.com";
        dr["phone"] = "0000000002";
        act.Rows.Add(dr);

        dr = act.NewRow();
        dr["id"] = 103;
        dr["name"] = "Robert";
        dr["email"] = "robert@mail.com";
        dr["phone"] = "0000000003";
        act.Rows.Add(dr);

        dr = act.NewRow();
        dr["id"] = 104;
        dr["name"] = "Kevin";
        dr["email"] = "kevin@mail.com";
        dr["phone"] = "0000000004";
        act.Rows.Add(dr);

        dr = act.NewRow();
        dr["id"] = 106;
        dr["name"] = "TomChen";
        dr["email"] = "tomchen@mail.com";
        dr["phone"] = "0000000005";
        act.Rows.Add(dr);

       var lselColumns = new[] {"id", "name"};
       var dt = act.DefaultView.ToTable(true, lselColumns);
       foreach (DataRow drow in dt.Rows)
       {
           string drowData = string.Empty;

           foreach (DataColumn r in drow.Table.Columns)
           {
               drowData += (drowData == string.Empty) ? drow[r] : "|" + drow[r];
           }
           Console.WriteLine(drowData);

       }
       Console.ReadLine();
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜