How do I display only certain columns from a data

2019-05-21 00:28发布

I'm using a web service that returns a dataset. in this dataset there are 5 table, let's say table A, B, C, D, E. I use table A.

So

DataTable dt = new DataTable()
dt = dataset.Table["A"]

Now in this datatable there are columns a1,a2,a3,a4,a5,a6,a7.

Let's say I only want to get columns a3 and a4 then bind it to my datagrid.

How do I do this?

6条回答
欢心
2楼-- · 2019-05-21 00:35

Hi Following code can be used

//It represent name of column for which you want to select records
string[] selectedColumns = new[] { "a3", "a4" }; 

DataTable tableWithSelectedColumns = new DataView(dataset.Table["A"]).ToTable(false,  selectedColumns);

I tried this and it works.

查看更多
太酷不给撩
3楼-- · 2019-05-21 00:42

I'd bind the whole table, then set up visibility of the coulmns as follows

dgvMain.Columns[ColumnA3_Name].Visible = true;
dgvMain.Columns[ColumnA1_Name].Visible = false;
查看更多
别忘想泡老子
4楼-- · 2019-05-21 00:48

I'd recommend reading this article from 4GuysFromRolla for anyone who needs a good understanding of the DataGrid Web Control.

Note: Since this question is already answered. I want to clarify what needs to be done, just in case anyone else is wondering.

DataSet ds;

//Get Data
using (SqlConnection connection = new SqlConnection(connectionString))
    {
        // Create the command and set its properties.
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandText = "GetMyData";
        command.CommandType = CommandType.StoredProcedure;

        ds = connection.ExecuteDataSet();
    }
if(ds !=null && ds.Tables.Count > 0)
{
    dg.DataSource = ds.Tables[0];
    // disable autogeneration of columns
    dg.AutoGenerateColumns = false;
    //Hide unecessary columns
    dg.Columns["a3"].Visible = false;
    dg.Columns["a4"].Visible = false;
}
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-05-21 00:49

Ignore the fact that you have more data than you need. Set AutoGenerateColumns to false. Create BoundColumns for a3 and a4.

查看更多
Ridiculous、
6楼-- · 2019-05-21 00:51

You can always try to set DataPropertyName properties of particular columns to match what's in your DataTable. Then bind that DataTable to a BindingSource and bind that binging source to your grid.

As long as names of columns in your DataTable match DataPropertyNames of your DataGrid columns, your data grid should display only those matched columns.

In my example my stred proc does something simle like:

ALTER PROCEDURE ps_Clients_Get
AS
BEGIN
    SELECT 
        convert(varchar(2000), path) as [Client Folder], 
        c.description as [Client Name],
        c.* 
    FROM Client c
END 
GO

and my C# code:

using (DataTable dt = new DataTable())
{
    using (OdbcConnection cnDsn = new OdbcConnection(cmLocalTrackingDBDSNAME))
    {
        cnDsn.Open();
        using (OdbcCommand cmdDSN = new OdbcCommand())
        {
                  var _with1 = cmdDSN;
                  _with1.Connection = cnDsn;
                  _with1.CommandType = System.Data.CommandType.StoredProcedure;
                  _with1.CommandText = "{ CALL ps_Clients_Get }";
                  using (OdbcDataAdapter adapter = new OdbcDataAdapter())
                  {
                            dt.Locale = System.Globalization.CultureInfo.InvariantCulture;
                            adapter.SelectCommand = cmdDSN;
                            adapter.Fill(dt);
                            bindingSourceDataLocation.DataSource = dt;
                            dataGridViewDataLocation.AutoGenerateColumns = false;

                            dataGridViewDataLocation.DataSource = bindingSourceDataLocation;
                  }
         }
         cnDsn.Close();
    }
}

Good luck!

查看更多
Summer. ? 凉城
7楼-- · 2019-05-21 00:56
    Dim DT As DataTable = YourDT

    DGV.DataSource = dt
    DGV.AutoGenerateColumns = False

    Dim cc = DGV.ColumnCount

    For i = 0 To cc - 1
        DGV.Columns(i).Visible = False
    Next

    DGV.Columns("ColumnToShow").Visible = True
    DGV.Columns("ColumnToShow").Visible = True
查看更多
登录 后发表回答