asp.net,c#.net grid view footer problem - asp.net

i have a gridview in web page
<asp:GridView ID="grid_search" runat="server" >
and it contains Footer for total value of Columns
i have a Column name Production value in Gridview
prod_val nvarchar(50)
for data bind i use the code behind
protected void grid_search_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
`tot_prodVal = tot_prodVal + Convert.ToDecimal(e.Row.Cells[2].Text);`
}
}
but it shows an error at debugging time that
System.FormatException: Input string was not in a correct format.
plz give me solution..

Since you have mentioned footer in your question, I am guessing you want to perform that operation in the footer. So your if condition should be
e.Row.RowType == DataControlRowType.Footer
I guess because you are using DataRow in your condition, e.Row.Cells[2].Text doesn't really point to the field you want it to.

Related

Cannot find id of control inside gridview which inside another gridview

Can anyone help me on this?
I have a button(ImageButton1) inside a GridView (gvSdt), and gridview b is inside another Gridview (gvClass).
but it come out an error said "The name 'gvSdt' does not exist in the current context"
The code i used as below:
protected void gvSdt_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = gvSdt.SelectedRow;
int rowIndex = gvSdt.SelectedIndex;
string strValue = gvSdt.DataKeys[rowIndex].Value.ToString();
}
Since gvSdt is contained within another GridView, you won't be able to access it directly, as it is repeated content. Since gvSdt can exist multiple times, gvSdt would therefore refer to multiple GridViews.
This is made easy however, since you are using the SelectedIndexChanged event of the GridView in question. Notice the sender argument of the method. The object that gets passed there is actually the GridView. So just cast it as such.
protected void gvSdt_SelectedIndexChanged(object sender, EventArgs e)
{
GridView gvSdt = (GridView)sender;
GridViewRow row = gvSdt.SelectedRow;
int rowIndex = gvSdt.SelectedIndex;
string strValue = gvSdt.DataKeys[rowIndex].Value.ToString();
}

Access data from BoundField DataField

how would I access data from <asp:BoundField DataField="DateStart".
I have an IF statement and I want to see if data is > or < than the data in the DataField.
I used to use rows(0).findControl but that wont work anymore.
If today > item.FindControl("btnSelect") And today < item.FindControl("btnSelect") Then
if its possible
You cannot use FindControl on BoundFields, only with TemplateFields. You need to use the cell's Text property:
Dim text = grid.Rows(index).Cells(index).Text ' the cell-index is the column-index '
You need to parse it to DateTime/Date:
Dim dateStart = Date.Parse(text)
If Date.Today > dateStart.Date ...
But if you use RowDataBound instead you can access the original DataItem. But then i need to know more to show you an example.
In the definition of your GridView, add
<asp:GridView .... DataKeyNames="ItemID" ...>
You also need to use OnRowDataBound, not OnDataBound
<asp:GridView .... DataKeyNames="ItemID" ... OnRowDataBound="GridView_RowDataBound">
Then in your code behind, something like this
protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
int ItemId = Int32.Parse(YourGridView.DataKeys[e.Row.RowIndex].Values[0].ToString());
}
}
If you want to find multiple value then set DataKeyNames as
DataKeyNames="ID,Name,COde,Value and so on"

How to store cell value of an Asp.Net GridView row on click in a C# variable

I am having a GridView in my page , I want that when a particular row is clicked I want the cell value of that particular row.Currently I get that that value by using something like this ,
protected void SearchRecordGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "location='ModifyCustomerByCategory.aspx?ApplicationNo=" + e.Row.Cells[0].Text + "'");
}
}
Currently I am passing that value as a parameter to the page , can any body tell how can I store that value in a variable or , atleast on click I assign it to a hidden field.
Thanks for any suggestions.
Try this link.
It runs through everything you will need, and on the gridview selectedindexchanged event you can reference the value with GridView.SelectedRow.Cells[0].Text or the control containing the value Gridview.SelectedRow.FindControl("[Control Name]")
In row data bound simply save that value and use it
int appno = CInt(e.Row.Cells[0].Text)
and if you want to use this value to next page
int appno = CInt(Request.Querystring("ApplicationNo"))

How can I put the return of a method into the GridView in ASP.NET?

I have a GridView that gets it's data from the SQL database.
I would like to alter some of it using an external method, something like this:
SQL in:
ID:0
Then, the altered method will be called with 0 as a parameter, and will return some string that will be shows in the GridView.
Thank you, Mark.
Attach a handler to the RowDataBound event. You have full control to all of the cells in the GridViewRow, and can modify the text anyway you want. Example from MSDN:
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
}
}
Reference at: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

ASP.Net Grid View rolling total in Gridview

I have seen several tutorials on how to achieve this.
However in my opinion they require a lot of prior knowledge on how to programatically refer to each item.
Does anyone have a link to or can create a relatively basic example of how to achive a running total in the footer for an ASP:Gridview?
This is what I use:
protected void InvoiceGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
var invoice = (Invoice) e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.Header)
{
totalAmt = 0;
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
totalAmt += invoice.Amount;
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
var amountTotalLabel = (TextBox) e.Row.FindControl("AmountTotalTextBox");
amountTotalLabel.Text = totalAmt.ToString("0.00");
}
}
TotalAmt is protected instance variable on the page. Not sure if it's what you were looking for based on your comment about "programmatic knowledge." But it works and is fairly straight-forward. The gridview is bound to a List<Invoice> in this case.
Add the footer Template and on the RowDataBound, have a global variable to store the summation sum,
At the e.Row.RowType = DataControlRowType.DataRow type do the summation , and # the e.Row.RowType = DataControlRowType.Footer store the vale in the appropriate cell
for further info look # MSDN LINK
This is how I do it. Very easy. You just sum the row that has your numbers in it and place it in the footer.
((Label)GridView.FooterRow.Cells[1].FindControl("your_label")).Text = ds.Tables[0].Compute("sum(Column_name)", "").ToString();
I think the method I use is pretty basic and doesn't require programatically referring to columns in the Gridview, if that's what you mean. That's one of the nice parts is that once you get the back-end functions written, you can add totals to any Gridview by only editing the .aspx file.
In your GridView, make the column like this:
<asp:TemplateField HeaderText="Hours">
<ItemTemplate><%#DisplayAndAddToTotal(Eval("Hours").ToString(), "Hours")%></ItemTemplate>
<FooterTemplate><%#GetTotal("Hours")%></FooterTemplate>
</asp:TemplateField>
The second parameter to DisplayAndAddToTotal can be any string you want as long as you use the same string in GetTotal. I usually just use the field name again though. Here are the two functions used, DisplayAndAddToTotal and GetTotal. They use a Hashtable to store the totals so that it works with any number of columns you want to add up. And they also work with counting the number of "True"s for a Boolean field.
Protected total As Hashtable = New Hashtable()
Protected Function DisplayAndAddToTotal(itemStr As String, type As String) As Double
Dim item As Double
If itemStr = "True" Then
item = 1
ElseIf Not Double.TryParse(itemStr, item) Then
item = 0
End If
If total.ContainsKey(type) Then
total(type) = Double.Parse(total(type).ToString()) + item
Else
total(type) = item
End If
Return item
End Function
Protected Function GetTotal(type As String) As Double
Try
Dim result As Double = Double.Parse(total(type).ToString())
Return result
Catch
Return 0
End Try
End Function

Resources