Click here to Skip to main content
15,920,650 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my project I want the variable 'v_total' to bind dynamically with 'gridview3'.
Hereunder is my code:

XML
</Columns>
       <RowStyle HorizontalAlign="Center" />
       <Columns>
       <asp:BoundField DataField="item_code" HeaderText="item_code"  />
</Columns>


protected void Button18_Click(object sender, EventArgs e)
{
v_total = v_total + 1;
}
protected void Button19_Click(object sender, EventArgs e)
{
SqlDataAdapter da1 = new SqlDataAdapter("select item_code from item", con);
SqlCommandBuilder cb21 = new SqlCommandBuilder(da1);
DataSet ds1 = new DataSet("member1");
da1.Fill(ds1, "member1");
GridView3.DataSource = ds1;
GridView3.DataBind();
GridView3.Visible = true;
}
Posted

1 solution

Hello,

The code as described above is slightly confusing. You have two event handlers. The first increases the variable v_total with 1, while the second fills a dataset with an SQL query from a database. Then, you bind that dataset as a datasource to the gridview. The bit that I'm missing is where you want to use the v_total in your gridview. You can only bind one datasource to your gridview so you'll have to bind your value directly to a column or cell. If you want to , for example, bind that value to a particular label in your gridview, you could try something like this :

XML
<asp:GridView ID="gridProgress" runat="server" AutoGenerateColumns="False">
    <Columns>
       <asp:TemplateField HeaderText="Total Value">
          <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" />
          <HeaderStyle HorizontalAlign="Center" />
          <ItemTemplate>
              <asp:Label ID="lblTotalValue" runat="server"></asp:Label>
          </ItemTemplate>
      </asp:TemplateField>
    </Columns>
</asp:GridView>

Use the FindControl property of Gridview :
Label lbl = GridView3.Rows[rowIndex].FindControl("lblTotalValue") as Label;
lbl.Text = Convert.ToString(v_total);

Of course, you could bind your value to other controls inside your gridview as well. You could also add your value v_total to the dataset ( or datatable (works cool as datasource as well)) and then bind it directly to the gridview. Hope this helps.
 
Share this answer
 
v2
Comments
S.Rajendran from Coimbatore 25-Sep-13 4:16am    
Actually I want to add the variable 'v_total' to existing dataset(DS1) and then bind to Gridview3 alongwith 'item_code' column. So, how to ?

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