我有一个 sql statemente,但我不想在 gridview (c# webforms) 中显示所有信息。如何在运行时在gridview上绑定特定列?
谢谢
首先,我会在 Visual Studio Designer 中编辑列,以便获得我想要的确切列。
这取决于您如何将数据提取到 DataGrid 中。如果您使用 SqlDataSource,我建议您关闭Selecting
事件。在其他情况下(SqlDataReader)我会在阅读时进行绑定。或者你也可以使用 Linq to Entity 来获取你想要的值,就像这样:
var name = from names in context.data_table
select names;
gridview.datasource = name;
您可以自己为 DataGridView 创建列。例子:
DataGridView dataGridView1 = new DataGridView();
BindingSource bindingSource1 = new BindingSource();
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "FieldA";
dataGridView1.Columns[0].DataPropertyName = "FieldA";
dataGridView1.Columns[1].Name = "FieldB";
dataGridView1.Columns[1].DataPropertyName = "FieldB";
bindingSource1.DataSource = GetDataTable();
dataGridView1.DataSource = bindingSource1;
Be sure to Data Bind your grid after you set a Data Source like shown above. Best of luck!
Entities entities = ObjectContextManager.GetCurrentContext<AdHocReportingEntities>();
var users = from e in entities.Users
where e.State == userState'
orderby e.Name
select new { e.Name, e.State};
Grid.DataSource = users;
Grid.DataBind();`