locked
Column Name RRS feed

  • Question

  • User-1499457942 posted

    Hi

     I have below code . Instead of index i want to give column name on below line

    e.Row.Cells[2].Text = (string)cmd.ExecuteScalar();

    protected void gvw_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //string m_StoreCategory = e.Row.Cells[2].Text;
                    string m_Category = string.Format("{0}", DataBinder.Eval(e.Row.DataItem, "Category"));
                    using (SqlConnection con = new SqlConnection(CommonFunction.connectionString))
                    {
                        SqlCommand cmd = new SqlCommand("Select Description from [Categories] where No = @Category", con);
                        cmd.Parameters.AddWithValue("@Category", m_Category);
                        cmd.CommandType = CommandType.Text;
                        if (con.State == ConnectionState.Closed)
                        {
                            con.Open();
                        }
                        e.Row.Cells[2].Text = (string)cmd.ExecuteScalar();
                    }
                }
            }

    Thanks

    Tuesday, August 21, 2018 3:37 PM

All replies

  • User-893317190 posted

    Hi JagjitSingh ,

    You could get the cell through headerText  property of the column, but the type of the cell should be DataControlFieldCell and you should get the cell through for each.

    Below is my code.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" >
                  <Columns>
                      <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
                      <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
                     
                      <asp:TemplateField HeaderText="myOwnText">
    
                          
                      </asp:TemplateField>
                  </Columns>
              </asp:GridView>
              <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address] FROM [Customers]"></asp:SqlDataSource>

    Code behind.

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if(e.Row.RowType==DataControlRowType.DataRow)
                {
                   
                    TableCellCollection tableCellCollection = e.Row.Cells;
                 
    
                    foreach (DataControlFieldCell item in tableCellCollection)
                    {
                        if (item.ContainingField.HeaderText == "myOwnText")
                        {
                      
                            item.Text = "hello";
                        }
                        if(item.ContainingField.HeaderText== "CustomerID")
                        {
                            item.Text = item.Text + "myEdit";
                        }
                    }
                }
              
            }

    The result.

    Best regards,

    Ackerly Xu

    Wednesday, August 22, 2018 2:50 AM