3

I have two textbox and one gridview in my webform. The gridview is binded with database. But I want to add one more column on runtime which will be the textbox input from the webform. Because the scenario is like: I am maintaining two formula to calculate some percentage using the two textbox and the client wants to see this calculation for each row in the gridview.

But I cannot do this.

Is there anyone who can help me on this please? May be some suggestion.

Thanks in advance.

1 Answer 1

3

You could add the column in your GridView markup with a label control to display the result as follows.

Here is the markup needed, please note Visible is set to false.

<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField Visible="false">
    <ItemTemplate>
        <asp:Label ID="label1" runat="server"></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Use the RowDataBound event to find the label and calculate your result as below:

void GridView1GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{ 
 if(e.Row.RowType == DataControlRowType.DataRow)
 {
  //find the control
  var label1 = e.Item.FindControl("label1") as Label;
  if (label1 != null)
  {
   if (!string.IsNullOrEmpty(tbInput1.Text) && !string.IsNullOrEmpty(tbInput2.Text))
   {
      // Do the calculation and set the label
      label1.Text = tbInput1.Text + tbInput2.Text;
      // Make the column visible
      GridView1.Columns[0].Visible = true;
   }
  }
 }
}

Please forgive any errors, I have not tested the above.

Sign up to request clarification or add additional context in comments.

1 Comment

sure bro..I would love to do this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.