Kamala Veni

Kamala Veni

  • NA
  • 65
  • 21.5k

how to add two columns and display in Grid view ?

Jun 9 2016 3:40 AM
Hi..
 
   I am having  sql table in dat am having two column name A and B .these two row values added and show in the c label as total value and dat grand total too plz help me
 
model:
 
A  B  C
 
3  4   7
1  1   2
4  5   9 
 total:18 (C total)
ex:
 
am having for add 1 column oly but  i need for two columns:
 
 
<asp:GridView ID="Grd1" runat="server" AutoGenerateColumns="false" CellPadding="5"
ForeColor="Black" ShowFooter="TRUE" OnRowDataBound="GridView1_RowDataBound"
Width="1108px" Height="224px" style="margin-right: 0px; margin-top: 0px; margin-left: 209px; text-align: left;">
<Columns>
 
<asp:TemplateField HeaderText="DRAWALVOLT">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Eval("DRAWALVOLT")%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<div style="text-align: right;">
<asp:Label ID="lbty" runat="server" >TOTAL</asp:Label>
</div>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CPPQUANTUM_C">
<ItemTemplate>
<div style="text-align: right;">
<asp:Label ID="lblqty" runat="server" Text='<%# Eval("CPPQUANTUM_C") %>' />
</div>
</ItemTemplate>
<FooterTemplate>
<div style="text-align: right;">
<asp:Label ID="lblTotalqty" runat="server" />
</div>
</FooterTemplate>
</asp:TemplateField>
</Columns>
 
 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblqy = (Label)e.Row.FindControl("lblqty");
int qty = Int32.Parse(lblqy.Text);
total = total + qty;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblTotalqty = (Label)e.Row.FindControl("lblTotalqty");
lblTotalqty.Text = total.ToString();
}
}
 
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["App"].ToString());
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("select COMPANYNAME,HTSCNO,EDC,DRAWALVOLT,(CPPQUANTUM_C) from TBL_CPPMASTER WHERE DRAWALVOLT like '%%' ", con);
DataSet ds = new DataSet();
int i = 0;
string sql = null;
string connetionString = "data source=VAAYUSEVA_SVR;Initial Catalog=cppsch;User ID=sa;Password=sa";
con.Open();
da.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
Grd1.DataSource = dt;
Grd1.DataBind();
}
}
 
 

Answers (4)