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

所以

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

现在在这个数据表中有列a1,a2,a3,a4,a5,a6,a7。

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

我该怎么做?

有帮助吗?

解决方案

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

其他提示

我建议您阅读4GuysFromRolla的这篇文章,以满足任何需要的人很好地理解 DataGrid Web控件。

注意:由于这个问题已经得到解答。我想澄清需要做些什么,以防万一其他人想知道。

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

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

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

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

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

在我的例子中,我的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

和我的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();
    }
}
祝你好运!

嗨以下代码可以使用

//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);

我试过这个并且有效。

    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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top