Find sum based on particular row id in asp.net gridview - asp.net

I have an asp.net Gridview which is having following columns and rows. The image has been attached
I am trying to find sum of quantities based on particular Item ID. In this case 57,53 & 91.

You can use the RowDataBound event for that.
//create a variable outside the rowdatabound method
int total_sum = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the row back to a datarowview
DataRowView row = e.Row.DataItem as DataRowView;
//or if a list<object> is bound to the gridview
//MyClass row = e.Row.DataItem as MyClass;
//cast the correct column to an int and add it to the total
total_sum += Convert.ToInt32(row["itemid"]);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
//show results. this wil work even if the footer is not shown
Label1.Text = string.Format("{0:N0}", total_sum);
}
}

Related

How to access dynmaically added Textbox values ASP.Net

I have a gridview placed on a ASP.Net WebSite, and added a column dynamically like this:
protected void gvGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (DataControlRowType.DataRow == e.Row.RowType && e.Row.RowState != DataControlRowState.Edit &&
(e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
TextBox tb = new TextBox();
tb.ID = "tb";
tb.Attributes.Add("runat", "server");
int i = e.Row.Cells.Count;
i = i - 1;
e.Row.Cells[i].Controls.Add(tb);
}
}
The gridview is populated, and at the end of every row the TextBox is added. When I look at the HTML Code that is delived to the Browser, I can see that the ID of every Textbox is "gv_Items_tb_X" with X being the current row index.
Now I would like to access the content that the user types in the tb on a button click. I tried following code, but it I get an exception becuase the textbox is null.
protected void btn_UpdateCount_Click(object sender, EventArgs e)
{
OI_Data_Service.OI_Data_ServiceClient sc = new OI_Data_Service.OI_Data_ServiceClient();
foreach (GridViewRow row in gv_Items.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
TextBox tb_value = (TextBox)gv_Items.Rows[row.RowIndex].Cells[5].FindControl("gv_Items_tb_" + row.RowIndex);
sc.UpdateItemOnOpening("1", row.Cells[0].Text, tb_value.Text);
}
}
Response.Redirect("~/okay.aspx");
}
Can anyone tell my what I am doing wrong?
Thanks!

Change values of a single column of a databound gridview and display it in the same gridview

Its a weird requirement but, Can I alter values of a single column of a databound gridview and display it in the same gridview ? Say, in GridView_RowDataBound(object sender, GridViewRowEventArgs e). I have edited the code for what I am actually doing. Problem is "DataItem" is a typeof class(entity fetched db table) and converting it to Datarow is not possible. So how do i go about it?
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (drv["Id"] != DBNull.Value)
{
string val = CommonUtil.Decrypt(drv["Id"].ToString());
e.Row.Cells[3].Text = val ;
}
}
}
Yes it's doable in GridView RowDataBound something like:
if(e.Row.RowType==DataControlRowType.DataRow)
{
//say you want to set value of 3rd column to "Hello"
// e.Row.Cells[2].Text="Hello"; //0 based index
MyCustomClass myitem= (MyCustomClass) e.Row.DataItem;
if (myitem.Id != null)
{
string val = CommonUtil.Decrypt(myitem.Id.ToString());
e.Row.Cells[3].Text = val ;
}
}
If that doesn't help, please share some code and more information on exactly what column you want to change and to what value.
You can cast to the type you want:
MyCustomClass drv = (MyCustomClass) e.Row.DataItem;

how can get column value in row edit event of gridview

This is my editing code in rowdatabound event.
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList dl = (DropDownList)e.Row.FindControl("Sectionname");
comp.MEDIUM = Convert.ToString(e.Row.FindControl("Medium"));
comp.CLASSNAME = Convert.ToString(e.Row.FindControl("ClassName"));
comp.ACADAMICYEAR = Convert.ToString(e.Row.FindControl("AcademicYear"));
DataTable worktype = inter.bindsectionforgird(comp);
dl.DataSource = worktype;
dl.DataTextField = "SectionName";
dl.DataValueField = "SectionId";
dl.DataBind();
}
}
still I am not able to get the value of those fields.
I suggest you to write your code in RowDataBound event
void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Write your code here
}
}
And you can find your Grid's columns as
e.Row.FindControl("yourControlID")
You can see detail in
MSDN reference 1
MSDN reference 2
CodeProject

ChildGridView column's Data On Binding

i am using nested gridview, on RowDatabound Event i am binding childgridview based on some condition of parentgridview column. meanwhile i need to know the column value of child gridview.how can i do that?
I am using this code:-
protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gridViewchild = (GridView)e.Row.FindControl("gridView2");
string custState = ((DataRowView)e.Row.DataItem)["CustState"].ToString();
if (!string.IsNullOrEmpty(custState))
{
gridViewchild .DataSource = MyDataSource;
gridViewchild .DataBind();
//now i need to know the childgridview column's data ????
}
}
}
There are couple of ways you can achieve this:
1- Loop through the rows of Child GridView
foreach (GridViewRow row in childGridView.Rows)
{
//row.Cells[index];
}
2- Use RowDataBound event of child GridView

How to add a row in asp.net grid view

So far I have done this, I am not sure whether this is right or wrong
public partial class _Default : System.Web.UI.Page
{
Label l = new Label();
GridView gv = new GridView();
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
GridViewRow gvr = new GridViewRow(i, i, DataControlRowType.DataRow, DataControlRowState.Normal);
gvr.Controls.Add(l);
gv. (what to do here)
}
this.Controls.Add(gv);
}
}
please help
gv.Rows.Add(gvr);
If you're starting with an empty GridView, an easier way to dynamically create x rows is to create a dummy list and then set it to the data source:
var list = new List<string>(10); // replace 10 with number of empty rows you want
// for loop to add X items to the list
gv.DataSource = list;
gv.DataBind();
If you are doing this, I'd recommend doing it with a Repeater. It's a lot easier to manage.
The DataGrid fires the RowCreate event when a new row is created.
Collapse
//OnRowCreated="GridView3_RowCreated"
protected void GridView3_RowCreated(object sender, GridViewRowEventArgs e)
{
//When a child checkbox is unchecked then the header checkbox will also be unchecked.
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState ==
DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
CheckBox chkBxSelect = (CheckBox)e.Row.Cells[1].FindControl("chkselect");
CheckBox chkBxHeader = (CheckBox)this.GridView3.HeaderRow.FindControl("chkHeader");
chkBxSelect.Attributes["onclick"] = string.Format("javascript:ChildClick(
this,'{0}');", chkBxHeader.ClientID);
}
}

Resources