Click here to Skip to main content
15,886,038 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is the query I am using to populate my grid view:
C#
string str = string.Format(@"select m.pcode, m.fyyear, m.date, m.salary, m.ta, m.contigency, m.nrc, m.institcharges, m.others, p.total FROM monthly AS m inner join project p on p.pcode=m.pcode where m.pcode = '{0}'", DropDownList1.SelectedItem.Value.ToString());

What I would like is that the value from total column is displayed in grid view footer on a label.
The value of total column is same along every row so it does not matter which ever cell we choose plus I would like to hide this column from actually being displayed in grid view. I only need to extract the value from this column.

Which event should I use?
Posted
Updated 16-Jan-13 8:50am
v3
Comments
pryashrma 16-Jan-13 23:30pm    
try using OnRowBound event
a2ulthakur 17-Jan-13 5:16am    
i have to get value from column total to a label25. The label 25 is inside gridview footer column 9th
pryashrma 17-Jan-13 7:53am    
please provide code

HI , u should try like that

C#
protected void gvHardware_RowDataBound(object sender, GridViewRowEventArgs e)
  {
      int intFirstYearAmt;
      
      if (e.Row.RowType == DataControlRowType.DataRow)
      {

          intFirstYearAmt = Convert.ToInt32(((DataRowView)e.Row.DataItem)["ColumnName"].ToString());
// _intSumFisrtYear is global variable so declare as a global. 
          _intSumFisrtYear += intFirstYearAmt;

      }
      if (e.Row.RowType == DataControlRowType.Footer)
      {

          Label lbl2010 = (Label)e.Row.FindControl("lblFooterFirstYear");

          lbl2010.Text = _intSumFisrtYear.ToString();

          _intSumFisrtYear = 0;
      }
  }


I HOPE THIS WILL HELP U
 
Share this answer
 
Comments
a2ulthakur 17-Jan-13 23:34pm    
if (e.Row.RowType == DataControlRowType.DataRow)
{
total = Convert.ToInt32((DataRowView)e.Row.DataItem["total"].ToString()); // its showing error in this line "cannot apply indexing with [] to an expression of type object.

}

if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblamount7 = (Label)e.Row.FindControl("Label27");
lblamount7.Text =total.ToString();
}
your syntax is wrong

//total = Convert.ToInt32((DataRowView)e.Row.DataItem["total"].ToString());

Write like this

total = Convert.ToInt32(((DataRowView)e.Row.DataItem)["total"].ToString());
 
Share this answer
 
Comments
a2ulthakur 17-Jan-13 23:54pm    
now its giving this error:

lblamount7.Text =total.ToString();// use of unassigned local variable 'total'
solanki.net 17-Jan-13 23:55pm    
send your code
a2ulthakur 18-Jan-13 0:07am    
the code behind part is this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int total;
if (e.Row.RowType == DataControlRowType.DataRow)
{
total = Convert.ToInt32((DataRowView)e.Row.DataItem["total"].ToString()); // its showing error in this line "cannot apply indexing with [] to an expression of type object.
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblamount7 = (Label)e.Row.FindControl("Label27");
lblamount7.Text =total.ToString();
}
}

this is the aspx of the hidden column
<asp:TemplateField HeaderText="Total Allocated" Visible="False">
<edititemtemplate>
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("total") %>'>

<itemtemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("total") %>'>



if u want i can email the whole page thanks for ur help!
solanki.net 18-Jan-13 0:27am    
// I all ready told u use global variable .improve your logic
//Declare global variable int _sum=0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { int total=0; if (e.Row.RowType == DataControlRowType.DataRow) { total = Convert.ToInt32(((DataRowView)e.Row.DataItem)["total"].ToString()); // its showing error in this line "cannot apply indexing with [] to an expression of type object.
_sum +=total;
} if (e.Row.RowType == DataControlRowType.Footer) { Label lblamount7 = (Label)e.Row.FindControl("Label27"); lblamount7.Text =_sum.ToString(); } }
a2ulthakur 18-Jan-13 0:29am    
i made it a global variable the page is running without errors but no value is being fetched :(
not sure............
study this...
<asp:gridview id="GridView1" onrowdatabound="RowDataBound_gv" /> 

protected void RowDataBound_gv(object sender, EventArgs e)
    {
        double total;
        foreach (GridViewRow gvr in gvPoDetails.Rows)
        {
            Label lblTot = (Label)gvr.Cells(9).Text;
            total+= double.Parse(lblTot.Text);
        }

        Label lblTotFooter = (Label)GridView1.FindControl("label25");
        lblTotFooter.Text=total.ToString();
    }
 
Share this answer
 
v2
//declaring global variable
static int total=0;

// under gridview RowDataBound Event

C#
if (e.Row.RowType == DataControlRowType.DataRow)
        {

            total = Convert.ToInt32(((DataRowView)e.Row.DataItem)["total"].ToString());

        }

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label Label27 = (Label)e.Row.FindControl("Label25");
            Label27.Text =total.ToString();
        }


this is what worked for me :)
 
Share this answer
 
v2
try this reference

http://www.agrinei.com/gridviewhelper/gridviewhelper_en.htm[^]

hope this help..
 
Share this answer
 

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