如何仅显示数据表中的某些列?

时间:2022-05-01 01:55:08

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.

我正在使用返回数据集的Web服务。在这个数据集中有5个表,比如表A,B,C,D,E。我使用表A.

So

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

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

现在在这个数据表中有列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.

假设我只想获取列a3和a4然后将其绑定到我的数据网格。

How do I do this?

我该怎么做呢?

6 个解决方案

#1


11  

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

忽略您拥有的数据超出了您的需求。将AutoGenerateColumns设置为false。为a3和a4创建BoundColumns。

#2


5  

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

对于需要很好地理解DataGrid Web控件的人,我建议您阅读4GuysFromRolla中的这篇文章。

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;
}

#3


1  

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

我绑定整个表格,然后按如下方式设置coulmns的可见性

dgvMain.Columns[ColumnA3_Name].Visible = true;
dgvMain.Columns[ColumnA1_Name].Visible = false;

#4


0  

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.

您始终可以尝试设置特定列的DataPropertyName属性以匹配DataTable中的内容。然后将该DataTable绑定到BindingSource并将该binging源绑定到您的网格。

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

只要DataTable中的列名与DataGrid列的DataPropertyNames匹配,您的数据网格就只显示那些匹配的列。

In my example my stred proc does something simle like:

在我的例子中,我的stred proc做了类似的事情:

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:

和我的C#代码:

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!

#5


0  

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.

我试过这个并且它有效。

#6


0  

    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

#1


11  

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

忽略您拥有的数据超出了您的需求。将AutoGenerateColumns设置为false。为a3和a4创建BoundColumns。

#2


5  

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

对于需要很好地理解DataGrid Web控件的人,我建议您阅读4GuysFromRolla中的这篇文章。

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;
}

#3


1  

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

我绑定整个表格,然后按如下方式设置coulmns的可见性

dgvMain.Columns[ColumnA3_Name].Visible = true;
dgvMain.Columns[ColumnA1_Name].Visible = false;

#4


0  

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.

您始终可以尝试设置特定列的DataPropertyName属性以匹配DataTable中的内容。然后将该DataTable绑定到BindingSource并将该binging源绑定到您的网格。

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

只要DataTable中的列名与DataGrid列的DataPropertyNames匹配,您的数据网格就只显示那些匹配的列。

In my example my stred proc does something simle like:

在我的例子中,我的stred proc做了类似的事情:

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:

和我的C#代码:

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!

#5


0  

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.

我试过这个并且它有效。

#6


0  

    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