overriding Alternatingrowstyle in gridview - asp.net

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;
}

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!

How can I get the previous row in gridview rowdatabound?

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.

getting column header text inside rowdatabound event

I need to get column header inside the DataControlRowType.Data of rowdatabound event of GridView and here's how I'm doing it:
((DataTable)((GridView)sender).DataSource).Columns[i].ColumnName
Is there another, possibly more concise than shown above? Just wanted to check out here.
if (e.Row.RowType == DataControlRowType.Header) {
LinkButton LnkHeaderText = e.Row.Cells[1].Controls[0] as LinkButton;
LnkHeaderText.Text = "Name";
}
protected void gvLista_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < gvLista.HeaderRow.Cells.Count; i++)
{
string cabecera = gvLista.HeaderRow.Cells[i].Text;
if (cabecera.Equals("ofertaactiva"))
{
int activo = int.Parse(e.Row.Cells[i].Text);
if (activo != 1)
{
e.Row.BackColor = System.Drawing.Color.SandyBrown;
}
break;
}
}
}
}
Here is a way
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "Date";
}
}
But the question is why some one would like to change the header name at run-time.
You can use bound-field with header name as follows (need auto generate column to false)
<Columns>
<asp:BoundField DataField="DateField" HeaderText="Date"
SortExpression="DateField" />
</Columns>
Edit-1
To get the column name change it
var columnName= e.Row.Cells[0].Text ;
At least in VB.NET 2010 this approach is flawed -- for some reason (PRB?) e.Row.Cells(x).Text = "" in the RowDataBound event.
Private Sub gvSymbolList_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvSymbolList.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
Dim i As Integer
For i = 0 To e.Row.Cells.Count - 1
Select Case e.Row.Cells(i).Text.ToLower
Case "symbol"
COL_SYMBOL = i
Case "description"
COL_DESCRIPTION = i
Case "last"
COL_CLOSE = i
Case "change"
COL_CHANGE = i
End Select
Next
End If

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

how to retain viewstate for the dynamic controls created in GRIDVIEW

i am creating dynamic textbox onRowCreated event in gridview control, however when i try to findcontrol i get null
here is how i am dong...
protected void gvORg_RowCreated(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
txBox txtReg = new TextBox();
txtReg.ID = "_registration" + e.Row.RowIndex + rowId.ToString();
txtReg.Text = reg.RegistrationToken;
e.Row.Cells[7].Controls.Add(txtReg);
}
}
}
protected void gvOrg_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
.....
....
TextBox _registration1 = gvOrg.Rows[e.RowIndex].Cells[7].FindControl("_registration" + e.RowIndex + rowId) as TextBox;
}
Have you tried to find it by:
GridView gv = (GridView)sender;
GridViewRow gvr = (GridViewRow)gv.Rows[e.RowIndex];
(TextBox)gvr.FindControl("_registration" + e.Row.RowIndex + "_" + reg.RegistrationId.ToString())
i able to fix my problem here

Resources