Determine Cell Index from Column Name

Discussion in 'ASP .Net Datagrid Control' started by dev648237923, Nov 14, 2006.

  1. dev648237923

    dev648237923 Guest

    I have a gridview and I call RowDataBound to change the backcolor of a cell
    in the Status colomn based on the contents of a field.
    I do that below by specifying the column's index by number (4 on my grid) --
    but I'd like to specify it by using the column's name ("Status" in my grid).
    How can I get the cell index by specifying a bound column's name?
    Thank you

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
    e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    //4 is the column that has Status -- I need a better way to choose this
    column
    e.Row.Cells[4].BackColor = System.Drawing.Color.FromName(
    DataBinder.Eval(e.Row.DataItem, "StatusColor").ToString());
    }
    }
     
    dev648237923, Nov 14, 2006
    #1
    1. Advertising

  2. You may write a little utility method that will loop through the gridview
    Columns collection to find a column with matching name. Then use the column
    index to locate the cell in the Row.Cells collection.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]


    "dev648237923" <> wrote in message
    news:...
    >I have a gridview and I call RowDataBound to change the backcolor of a cell
    >in the Status colomn based on the contents of a field.
    > I do that below by specifying the column's index by number (4 on my
    > grid) -- but I'd like to specify it by using the column's name ("Status"
    > in my grid).
    > How can I get the cell index by specifying a bound column's name?
    > Thank you
    >
    > protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
    > e)
    > {
    > if (e.Row.RowType == DataControlRowType.DataRow)
    > {
    > //4 is the column that has Status -- I need a better way to choose this
    > column
    > e.Row.Cells[4].BackColor = System.Drawing.Color.FromName(
    > DataBinder.Eval(e.Row.DataItem, "StatusColor").ToString());
    > }
    > }
    >
    >
    >
    >
     
    Eliyahu Goldin, Nov 15, 2006
    #2
    1. Advertising

  3. Hi,

    You have two options to achieve that:

    1) You could use declarative way to do that without writing code-behind:

    <asp:TemplateField>
    <ItemTemplate>
    <asp:Label ID="lblStatus" Text='<%# Eval("status")
    %>' runat="server" BackColor='<%#
    System.Drawing.Color.FromName(Eval("statuscolor").ToString())
    %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>


    2) We can iterate the Cells collection of the Row and get the cell by the
    containing DataField name:

    <asp:BoundField DataField="status" />

    protected void OnRowDataBound(Object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    GetCellByBoundFieldName(e.Row, "status").BackColor =
    Color.FromName(DataBinder.Eval(e.Row.DataItem, "StatusColor").ToString());
    }
    }

    DataControlFieldCell GetCellByBoundFieldName(GridViewRow row, string
    fieldName)
    {
    foreach (DataControlFieldCell cell in row.Cells)
    {
    BoundField bf = cell.ContainingField as BoundField;
    if (bf != null)
    {
    if (bf.DataField == fieldName)
    {
    return cell;
    }
    }
    }
    return null;
    }



    Let me know if this answers your question.


    Sincerely,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications. If you are using Outlook Express, please make sure you clear the
    check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
    promptly.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Nov 15, 2006
    #3
  4. dev648237923

    Cider Guest

    "Walter Wang [MSFT]" wrote:

    > Hi,
    >
    > You have two options to achieve that:
    >
    > 1) You could use declarative way to do that without writing code-behind:
    >
    > <asp:TemplateField>
    > <ItemTemplate>
    > <asp:Label ID="lblStatus" Text='<%# Eval("status")
    > %>' runat="server" BackColor='<%#
    > System.Drawing.Color.FromName(Eval("statuscolor").ToString())
    > %>'></asp:Label>
    > </ItemTemplate>
    > </asp:TemplateField>
    >
    >
    > 2) We can iterate the Cells collection of the Row and get the cell by the
    > containing DataField name:
    >
    > <asp:BoundField DataField="status" />
    >
    > protected void OnRowDataBound(Object sender, GridViewRowEventArgs e)
    > {
    > if (e.Row.RowType == DataControlRowType.DataRow)
    > {
    > GetCellByBoundFieldName(e.Row, "status").BackColor =
    > Color.FromName(DataBinder.Eval(e.Row.DataItem, "StatusColor").ToString());
    > }
    > }
    >
    > DataControlFieldCell GetCellByBoundFieldName(GridViewRow row, string
    > fieldName)
    > {
    > foreach (DataControlFieldCell cell in row.Cells)
    > {
    > BoundField bf = cell.ContainingField as BoundField;
    > if (bf != null)
    > {
    > if (bf.DataField == fieldName)
    > {
    > return cell;
    > }
    > }
    > }
    > return null;
    > }
    >
    >
    >
    > Let me know if this answers your question.
    >
    >
    > Sincerely,
    > Walter Wang (, remove 'online.')
    > Microsoft Online Community Support
    >
    > ==================================================
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications. If you are using Outlook Express, please make sure you clear the
    > check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
    > promptly.
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    > ==================================================
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >

    Is there is no other way to get the value in the gridview cell by field name
    than to loop through each cell in the row? I remember in the past (probably
    in Windows forms) I was able to
    row.cells[DataGrid.Columns.IndexOf("fieldName")].text to get the value.
     
    Cider, Nov 21, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWlrZQ==?=

    get value by index and not column name...

    =?Utf-8?B?TWlrZQ==?=, Feb 1, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    2,886
    Karl Seguin
    Feb 2, 2005
  2. DBLWizard
    Replies:
    1
    Views:
    8,188
    Eliyahu Goldin
    Jul 19, 2005
  3. msnews

    use column name instead of index

    msnews, Sep 28, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    400
    Eliyahu Goldin
    Sep 28, 2005
  4. gane
    Replies:
    0
    Views:
    164
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    337
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page