Click here to Skip to main content
15,904,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
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)
{

}


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



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
Posted
Updated 12-Jun-17 23:42pm

1 solution

protected void grdOutstandingInvoice_RowDataBound(object sender, GridViewRowEventArgs e)
{
var index = GetColumnIndexByName(grid,name);
}

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;

}
 
Share this answer
 
v4
Comments
Member 10192073 13-Jun-17 5:46am    
var index = GetColumnIndexByName(grdOutstandingInvoice,"Ref Doc#");
but i don't want to find by header name
directly poNo how i can give?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900