如何仅在datagridview中绑定某些列 [英] how to bind certain column only in datagridview
本文介绍了如何仅在datagridview中绑定某些列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已成功从web服务绑定datagridview中的数据。但它绑定了所有列。但我只希望绑定一些列..
i have successfully bind data in datagridview from web service. but it binds all the columns. but i want only some columns to be binded..
推荐答案
在gridview属性中设置autogeneratecolumns =false.. :)
并使用boundfields绑定gridview .. :)
喜欢这样
Set autogeneratecolumns="false" in gridview property..: )
And bind gridview using boundfields.. :)
like that
<asp:gridview id="gvMessages" runat="server" autogeneratecolumns="false" xmlns:asp="#unknown">
CaptionAlign="NotSet" CellPadding="5">
<columns>
<asp:boundfield headertext="FielsHeader1" datafield="Fields1" />
<asp:boundfield headertext="FielsHeader2" datafield="Fields1" />
</columns>
</asp:gridview>
希望这可以帮到你
Hope this helps you
[WebMethod]
public static List<record> GetData(string param)
{
SqlConnection con = new SqlConnection(@"ConnectionString");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT type,desc,num,ref,date FROM foo", con);
SqlDataReader dr = cmd.ExecuteReader();
List<record> records = new List<record>();
while (dr.Read())
{
records.Add(new Record() {
AccountType = dr.GetString(0),
PartDescription = dr.GetString(1),
PartNumber = dr.GetInt32(2),
OrderRef = dr.GetString(3),
TransactionDate = dr.GetDateTime(4)
});
}
dr.Close();
con.Close();
return records;
}</record></record></record>
如果你得到的结果是DataTable
格式
你可以删除像这样不需要的列
IF you are getting the result inDataTable
format
you can remove the unwanted columns like this
string[] columnsToBeRemoved = { "column1", "Column2" };
foreach (string column in columnsToBeRemoved)
{
bool isValidColumn= dt.Columns.OfType<DataColumn>().Select(k => k.ColumnName).Any(k => k == column);
if (isValidColumn)
dt.Columns.Remove(column);
}
这篇关于如何仅在datagridview中绑定某些列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文