protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
**if (e.Row.RowIndex >= gridview1.PageSize) // ROW FOOTER TOTAL**
{
e.Row.BackColor = System.Drawing.Color.Firebrick;
e.Row.ForeColor = System.Drawing.Color.White;
}
}
This code works sometimes, someone can help me
DM,cheers
You can try to find the last column in the PreRender event
protected void grid_PreRender(object sender, EventArgs e)
{
GridViewRow row = grdAlert.Rows[grdAlert.Rows.Count - 1];
// do stuff with your row
}
If you just need to change the style of the footer you can use
<asp:GridView ID="grid" runat="server" FooterStyle="your style"></asp:GridView>
A grid view doesn't appear to have a row count until it's finished binding each row. So, another thought:
Can you determine the number of rows from the datatable that the gridview is binding to, then store that in a variable for use later?
you can find last row like this
GridViewRow row = GridView1.Rows[GridView1.Rows.Count-1];
or use this
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView grid = (GridView)sender;
if(e.Row.RowIndex == (grid.Rows.Count - 1))
{
//last row
}
}
}
It seems that you want to detect the footer row in RowDataBound since you have commented //ROW FOOTER TOTAL, you just have to check for the DataControlRowType.Footer:
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
// here it is
}
}
Otherwise you could compare the RowIndex with the row of the undrlying DataItem:
DataRowView row = (DataRowView)e.Row.DataItem;
if (e.Row.RowIndex == row.DataView.Count - 1) ; // last row
In your page load method set colors as below
protected void Page_Load(object sender, EventArgs e)
{
//code what you currently have ....
// add below code after that
GridViewRow row = GridView1.Rows.Count-1;
row.BackColor = System.Drawing.Color.Firebrick;
row.ForeColor = System.Drawing.Color.White;
}
Related
Below is my code behind:
public void enableEditMedChange(object sender, GridViewEditEventArgs e)
{
MedChangeTable.EditIndex = e.NewEditIndex;
}
protected void MedChangeTable_RowDataBound(object sender, GridViewRowEventArgs e)
{
using (DeveloprodDataClassDataContext adminDB = new DeveloprodDataClassDataContext())
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList newMedChangeChangeDD = (DropDownList)e.Row.FindControl("NewMedChangeChangeDD");
newMedChangeChangeDD.SelectedValue = ???
}
}
}
}
I would like to set the drop down's selected value to whatever value was in one of the cells in the row who's edit button was clicked on. How can I pass along this value to the row data bound event?
You can get the value like this:
DropDownList newMedChangeChangeDD = (DropDownList)e.Row.FindControl("NewMedChangeChangeDD");
newMedChangeChangeDD.SelectedValue = DataBinder.Eval(e.Row.DataItem, "YourDataFieldName").ToString();
More informations here.
I need to find count of total number of columns in a gridview inside the event rowdatabound .Is there any way for it.
Below is my few code:
protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{
LinkButton lnkView = new LinkButton();
lnkView.ID = "lnkView";
lnkView.Text = "View";
lnkView.Click += ViewDetails;
e.Row.Cells[3].Controls.Add(lnkView);
}
You can cast sender to GridView and get the count.
protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(!(sender is GridView))
return;
GridView gridView = (GridView) sender;
var colCount = gridView.Columns.Count;
//Your code
}
Not sure why you want this, but Cells is an array of the cells in this row, so to get the total number of columns:
protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{
var colCount = e.Row.Cells.Count;
}
I want to change the CSS styling of the first row in a gridview:
protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow firstRow = hoursReportGridView.Rows[0];
firstRow.CssClass = "firstRow";
}
}
I am getting this error: Index was out of range. Must be non-negative and less than the size of the collection.
In every instance there are multiple rows returned so I don't understand the issue
'
I assume the first row exists in GridView.Rows after RowDataBound. So you can access it afterwards. So i would use DataBound instead. Note that you also set the first row on every row since RowDataBound is triggered for every row in the grid.
protected void hoursReportGridView_DataBound(object sender, EventArgs e)
{
if(this.hoursReportGridView.Rows.Count > 0)
hoursReportGridView.Rows[0].CssClass = "firstRow";
}
Another option is to use GridViewRow.RowIndex
protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(e.Row.RowIndex == 0)
e.Row.CssClass = "firstRow";
}
}
Try this:
protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == 0)
{
e.Row.CssClass = "firstRow";
}
}
Why do you want to do that on the databind for every row?
Just do it in Page_Load:
// Run this after any binding calls, obviously
if(hoursReportGridView.Rows.Count > 0)
{
hoursReportGridView.Rows[0].CssClass = "firstRow";
}
I have a simple for each loop checking the text in the 10th cell of my gridview, then setting the colour of that cell to green or red dependant in the text.
This is working fine apart from the very first cell in the first row is being ignored. Ive had similar situations to this with for loops, but not a for each.
Heres my code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView vg = GridView1;
foreach (GridViewRow row in vg.Rows)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[10].Text == "Order has been dispatched.")
{
e.Row.Cells[10].BackColor = Color.LawnGreen;
}
if (e.Row.Cells[10].Text == "Order is being processed.")
{
e.Row.Cells[10].BackColor = Color.Red;
}
}
}
}
I don't know if this will help. Probably not. But you have redundant code. Change your code to the following and make sure the event handler gets called for each row. I don't think you have to make sure the GridViewRow.RowType is a DataRow, since you will only get this event on a DataRow.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells[10].Text == "Order has been dispatched.")
e.Row.Cells[10].BackColor = Color.LawnGreen;
if (e.Row.Cells[10].Text == "Order is being processed.")
e.Row.Cells[10].BackColor = Color.Red;
}
I am a new ASP.NET developer and I am trying to change the value of the first cell in each row in the GridVie by giving it a number starting from 1. It means that I want to add order to the list of things that will be displayed in the GridView.
I am using GridView RowDataBound method but now I don't know to set the limit of the for loop in it. Could anyone help me with this?
My code-behind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
for(int i=1, i<; i++)
e.Row.Cells[0].Text = i;
}
}
If your requirement is "change the value of the first cell in each row", it would be more like:
private int _rowIndex=0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
e.Row.Cells[0].Text = _rowIndex.ToString();
_rowIndex++;
}
}
You can try this, it will return of cell count:
int cells = ((TableRow) (e.Row)).Cells.Count;