如何在C#中的网格视图中按数据库列名查找列索引 [英] How to find a column index by database column name in grid view in C#
问题描述
我想通过绑定数据库列名找到一列索引任何人都可以帮助我如何实现这个功能
I want to find a column Index by bind Database column name can anyone help me how to achieve this functionality
<asp:TemplateField HeaderText="Ref Doc#">
<ItemTemplate>
<%# Eval("poNo")%>
</ItemTemplate>
<ItemStyle CssClass="textAlignCenter noWrap" />
<HeaderStyle CssClass="textAlignCenter noWrap" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Ref Date">
<ItemTemplate>
<%# Eval("poDate", "{0:dd-MMM-yyyy}")%>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblSum" runat="server" Text="Total Amount:" Font-Bold="true"></asp:Label>
</FooterTemplate>
<ItemStyle CssClass="textAlignCenter noWrap" />
<HeaderStyle CssClass="textAlignCenter noWrap" />
<FooterStyle CssClass="textAlignLeft" />
</asp:TemplateField>
protected void grdOutstandingInvoice_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
我想通过Eval(poNo)找到列索引,我们如何实现这一点。请帮帮我。
预付款
我尝试了什么:
我试过硬编码
I want to find column index by Eval("poNo"), how we can achieve this. please help me.
thanks in Advance
What I have tried:
I tried hard coded that
grdOutstandingInvoice.Columns[7].Visible = false;
但是根据要求他们增加了一些列,
然后我试了这个
but as per requirement they added some more columns,
then I tried this
private int GetColumnIndexByName(GridView grid, string name)
{
foreach (DataControlField col in grid.Columns)
{
if (col.HeaderText.ToLower().Trim() == name.ToLower().Trim())
{
return grid.Columns.IndexOf(col);
}
}
return -1;
}
protected void grdOutstandingInvoice_RowDataBound(object sender, GridViewRowEventArgs e)
{
var index = GetColumnIndexByName(grid,name);
}
但早期的标题名称是Reference Doc,现在他们通过Ref Doc更改了它#这就是为什么我不想冒未来的风险,我想通过我永远不会改变的数据库列字段找到列索引
But earlier header name was Reference Doc now they changed it by Ref Doc# that's why i dont want to take risk for future, i want to find column index by my database column field that will never change
推荐答案
protected void grdOutstandingInvoice_RowDataBound(object sender, GridViewRowEventArgs e)
{
var index = GetColumnIndexByName(grid,name);
}
用以下内容替换上面的函数
protected void grdOutstandingInvoice_RowDataBound(object sender, GridViewRowEventArgs e)
{
var index = GetColumnIndexByName(grdOutstandingInvoice,poNo);
}
如果不能正常工作,请尝试使用以下功能
< pre> static public int GetColumnIndexByDBName( GridView grdOutstandingInvoice,String poNo)
{
System.Web.UI.WebControls.BoundField DataColumn;
for(int Index = 0; Index< grdOutstandingInvoice.Columns.Count; Index ++)
{
DataColumn = grdOutstandingInvoice.Columns [Index] as System.Web.UI。 WebControls.BoundField;
if(DataColumn!= null)
{
if(DataColumn.DataField = = poNo)
返回索引;
}
}
返回-1;
}
Replace the above function with the following
protected void grdOutstandingInvoice_RowDataBound(object sender, GridViewRowEventArgs e)
{
var index = GetColumnIndexByName(grdOutstandingInvoice,poNo);
}
if it is not working, please try with the following function
<pre>static public int GetColumnIndexByDBName(GridView grdOutstandingInvoice, String poNo)
{
System.Web.UI.WebControls.BoundField DataColumn;
for (int Index = 0; Index < grdOutstandingInvoice.Columns.Count; Index++)
{
DataColumn = grdOutstandingInvoice.Columns[Index] as System.Web.UI.WebControls.BoundField;
if (DataColumn != null)
{
if (DataColumn.DataField == poNo)
return Index;
}
}
return -1;
}
这篇关于如何在C#中的网格视图中按数据库列名查找列索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!