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
Related
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);
}
}
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!
I would like to check the previous row data if it is equal to --,
if it is not equal to -- then I would enable button in the next row
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (DataBinder.Eval(e.Row.DataItem, "time_start").ToString() == "--")
{
Button btn = ((Button)e.Row.FindControl("Edit_Button"));
btn.Enabled = false;
}
}
}
You can also do it like this using GridView1.Rows[e.Row.RowIndex - 1].
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow prevrow = GridView1.Rows[e.Row.RowIndex - 1];
if( prevrow.RowType == DataControlRowType.DataRow)
{
// Your code for manipulating prevrow
}
if (DataBinder.Eval(e.Row.DataItem, "time_start").ToString() == "--")
{
Button btn = ((Button)e.Row.FindControl("Edit_Button"));
btn.Enabled = false;
}
}
}
One way to do this is:
Create a field previousRow in your class, of type GridViewRow.
Initialize this field to null in the a GridView.DataBinding event handler. This event is fired when databinding starts, before any of the RowDataBound events fires.
In your GridView.RowDataBound event handler, do your processing (including comparing with previousRow), then set previousRow = e.Row.
I'm designing a logistics system in ASP.Net . In the Order processing page, orders are displayed by Grid View,i want to change the font style of the rows to BOLD, which are marked as"ordedr not processed". thanks.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string OrStatus = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Orderstatus"));
if (OrStatus == "Order not processed")
{
//You can use whatever you want to play with rows
e.Row.Cells[0].Font.Bold = true;
e.Row.Cells[2].CssClass = "gridcss";
}
}
}
Follow that code. It will helps
You can do this in "rowdatabound" event of grid.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView grid = GridView1;
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
string orderstatus= Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Orderstatus"));
if(orderstatus=="Order not processed)
{
//write your code to change css
}
}
}
I have a GridView which uses the Alternatingrowstyle property, but I also would like to higlight each row when the user Edit the row, but using this code, it only highlights the rows that don't have the Alternatingrowstyle.
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.Rows[e.NewEditIndex].BackColor = System.Drawing.Color.Yellow;
gv.EditIndex = e.NewEditIndex;
if (e.NewEditIndex % 2 == 0)
{
gv.Rows[e.NewEditIndex].BackColor = System.Drawing.Color.Yellow;
}
}
I have a lot of success with gridview highlighting using Matt Berseth's samples and in particular, this extender
if ((e.Row.RowType == DataControlRowType.DataRow & ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit))) {
e.Row.BackColor = Drawing.Color.Yellow;
}