Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server
This is the query I am using to populate my grid view:
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 16-Jan-13 8:44am
Edited 16-Jan-13 8:50am
v3
Comments
pryashrma at 16-Jan-13 23:30pm
   
try using OnRowBound event
a2ulthakur at 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 at 17-Jan-13 7:53am
   
please provide code
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

HI , u should try like that
 
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
  Permalink  
Comments
a2ulthakur at 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();
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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());
  Permalink  
Comments
a2ulthakur at 17-Jan-13 23:54pm
   
now its giving this error:
 
lblamount7.Text =total.ToString();// use of unassigned local variable 'total'
solanki.net at 17-Jan-13 23:55pm
   
send your code
a2ulthakur at 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">

<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("total") %>'>


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


 
if u want i can email the whole page thanks for ur help!
solanki.net at 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 at 18-Jan-13 0:29am
   
i made it a global variable the page is running without errors but no value is being fetched :(
solanki.net at 18-Jan-13 0:39am
   
Put the break point and check value is coming or not in total .
solanki.net at 18-Jan-13 0:41am
   
and declare _sum as global
static int _sum=0
a2ulthakur at 18-Jan-13 0:46am
   
ok i did put the breakpoints on and the total variable has the value of the column its just that its not displaying it. one more thing i need to ask my column from which i am trying to reterive value is a hidden template field and so i m trying to display value in another column called grand total in there i have inserted a label, will it be able to show my value there ?
a2ulthakur at 18-Jan-13 0:48am
   
its working thanks, m sorry it was my little mistake :) i was giving the wrong label id thats it ! thanks a ton man :)
solanki.net at 18-Jan-13 1:03am
   
your welcome
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

try this reference
 
http://www.agrinei.com/gridviewhelper/gridviewhelper_en.htm[^]
 
hope this help..
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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();
    }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

//declaring global variable
static int total=0;
 
// under gridview RowDataBound Event
 
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 Smile | :)
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Dnyaneshwar@Pune 604
1 thatraja 370
2 OriginalGriff 306
3 Gihan Liyanage 224
4 kbrandwijk 204
0 OriginalGriff 737
1 Dnyaneshwar@Pune 604
2 Kornfeld Eliyahu Peter 505
3 Sergey Alexandrovich Kryukov 405
4 thatraja 370


Advertise | Privacy | Mobile
Web03 | 2.8.140902.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100