How do test for an alternating row in DataBound - c#

This code inserts a row under the first row that i choose to edit but when i edit the second row now new row appears can someone tell me or show me how to correct this. I've tried doing e.Row.RowInex + 1 and i get no rows. but if i do e.Row.RowInex + 2 i get a new row for the first but not the second.
protected void PageSettings_DataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0))
{
GridViewRow row = new GridViewRow(e.Row.RowIndex + 2, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
row.Cells.AddRange(CreateCells());
Table table = e.Row.Parent as Table;
table.Rows.AddAt(e.Row.RowIndex + 2, row);
}
}
private TableCell[] CreateCells()
{
TableCell[] cells = new TableCell[2];
TableCell cell;
cell = new TableCell();
cell.ColumnSpan = 2;
cells[0] = cell;
cell = new TableCell();
cell.ColumnSpan = 4;
cells[1] = cell;
return cells;
}
Solved for any one who has this trouble
((e.Row.RowState & DataControlRowState.Edit) > 0)

For a response to your title question: 'How do test for an alternating row in DataBound':
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Alternate)
{
// alternate rows
}
}
}

Related

GridView: OnRowDatabound HeaderText Equals to ColumnText with AutoGenerateColumn True c# Asp.net

I have a gridview with first columns is same as headers i.e. same value with autogeneratecolumns as true, what i need to do in RowDatabound if HeaderText Equals to Intersect of the first Column text, Change the color to Yellow. Please see the attached image of the Desireed Gridview Output.
GridViewDesiredOutput
HTML
<asp:GridView ID="GvSamples" OnRowDataBound="GvSamples_RowDataBound" runat="server" AutoGenerateColumns="True">
C#
public void BindSamplesGrid()
{
DataTable _dt = new DataTable();
_dt = BL.GetSample(_conn);
GvSamples.DataSource = _dt;
GvSamples.DataBind();
}
protected void GvSamples_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text == e.Row.Cells[i].Text)
e.Row.Cells[i].Text = e.Row.Cells[i].Text.Replace("_", " ");
}
}
}
I am using asp.net C# Gridview.
Thank you
There are a couple of issues with your code. First of all you can only access the header row in DataControlRowType.Header, so coloring the cells as desired must be done in DataControlRowType.DataRow.
Next you are evaluating exactly the same value, so it will always be true: e.Row.Cells[i].Text == e.Row.Cells[i].Text, although this does not seem to do much except replace a _, which has nothing to do with getting the cell color to be yellow.
The below snippet does what you need. It gets the current row number and colors the correct cell yellow.
protected void GvSamples_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//get the current row number
int rowIndex = e.Row.RowIndex;
//check if the rownumber does not exceed the column count
if (rowIndex < e.Row.Cells.Count - 1)
{
//make the cell yellow
e.Row.Cells[rowIndex + 1].BackColor = Color.Yellow;
}
}
}

Highlight gridview row based on datatable values

I need to highlight gridview rows based on values from datatable.
I have highlighted values like if any particular cell values has met some conditions then I can highlight using this code.
if(int.Parse(DataBinder.Eval(e.Row.DataItem,"Risk").ToString()) > 100)
{
e.Row.BackColor = Color.FromName("#FAF7DA");
}
Now my questions is, in my rowdatabound event I want to check values in datatable and I need to highlight values in the gridview.
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = DataRepository.highlightRow();
string[] strInactive = dt.AsEnumerable().Select(row => row.Field<string>("product_id")).ToArray();
foreach (GridViewRow row in gvProducts.Rows)
{
for (int i = 0; i < gvProducts.Columns.Count; i++)
{
if (gvProducts.Rows[0].Cells[0].Text.Contains("how to pass array values"))
{
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
}
}
For ex:
Here the datatable will return only one column values like this,10, 20, 20. Then I need to highlight rows of these values in the gridview.
you should be able to check e.Row.DataItem for values that meet your criteria
You can do something like this in the RowDataBound event:
if (e.Row.RowType == DataControlRowType.DataRow) {
DataRowView drv = (DataRowView) e.Row.DataItem;
if( drv("Risk") == <some condition> ) {
e.Row.BackColor = Drawing.Color.Black // :)
}
}
Perhaps this is what you are looking for:
private const System.Drawing.Color HIGHLIGHT = System.Drawing.Color.Yellow;
private const System.Drawing.Color NORMAL = System.Drawing.Color.White;
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = DataRepository.highlightRow();
string[] strInactive = dt.AsEnumerable().Select(row => row.Field<string>("product_id")).ToArray();
foreach (var value in strInactive)
{
e.Row.BackColor = (e.Row.Cells[0].Text == value) ? HIGHLIGHT : NORMAL;
}
}
}
I do not know what is in your DataTable, so I don't know what you want to test for.

Programatically choosing a variable number of gridview rows to format

I have a gridview where I format a specific number of cells for specific rows:
protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == 1 || e.Row.RowIndex == 2)
{
for (int i = 0; i < 14; i++)
{
e.Row.Cells[i].ForeColor = Color.Black;
e.Row.Cells[i].BackColor = ColorTranslator.FromHtml("#EAFDB3");
e.Row.Cells[i].Font.Bold = true;
}
}
}
}
Right now, the rows that get changed are static (row 1 and 2).
I've added functionality to the gridview and now need a dynamic number of rows formatted (between 2 and 10). The rows will always be next to eachother.
How can choose the rows dynamically?

Gridview Dynamic formatting according to conditions

I have this code on RowDataBound that alternates the color of the cells and also add an onclick event on the cells. The data comes dynamically from a stored procedure so the gridview has only one control. The ButtonField.
I want to have white color for cells that hasn’t any data inside, how can I achieve this?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
{
string back1 = "url('Images/GridBack1.jpg')";
string back2 = "url('Images/GridBack2.jpg')";
if (e.Row.RowType != DataControlRowType.DataRow)
return;
for (int i = 0; i < e.Row.Cells.Count; i++)
{
TableCell Cell = e.Row.Cells[i];
// if both row and column are odd, color them black
// if both row and column are even, color them white
if (((e.Row.RowIndex % 2 == 1) && (i % 2 == 1)) ||
((e.Row.RowIndex % 2 == 0) && (i % 2 == 0)))
{
if (string.IsNullOrEmpty(e.Row.Cells[i].Text)) //Edit
{
e.Row.Cells[i].BackColor = Color.Blue;
e.Row.Cells[i].ForeColor = Color.White;
}
else
{
e.Row.Cells[i].Width = Unit.Pixel(450);
e.Row.Cells[i].Height = Unit.Pixel(67);
e.Row.Cells[i].Style.Add("background-image", back1);
//e.Row.Cells[i].Style.Add("width", "150px");
//e.Row.Cells[i].Style.Add("word-wrap","break-word");
e.Row.Cells[i].Attributes.Add("align", "center");
}
}
else
{
if (string.IsNullOrEmpty(e.Row.Cells[i].Text)) //Edit
{
e.Row.Cells[i].BackColor = Color.Blue;
e.Row.Cells[i].ForeColor = Color.White;
}
else
{
e.Row.Cells[i].Width = Unit.Pixel(450);
e.Row.Cells[i].Height = Unit.Pixel(67);
e.Row.Cells[i].Style.Add("background-image", back2);
//e.Row.Cells[i].Style.Add("width", "150px");
//e.Row.Cells[i].Style.Add("word-wrap", "break-word");
e.Row.Cells[i].Attributes.Add("align", "center");
}
}
string color = "#000000";
e.Row.Cells[0].Attributes.Add("Style", "background-color: " + color + ";");
e.Row.Cells[0].Width = Unit.Pixel(150);
e.Row.Cells[0].Height = Unit.Pixel(67);
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
// Add events to each editable cell
for (int columnIndex = 2; columnIndex < e.Row.Cells.Count; columnIndex++)
{
// Add the column index as the event argument parameter
string js = _jsSingle.Insert(_jsSingle.Length - 2, columnIndex.ToString());
// Add this javascript to the onclick Attribute of the cell
e.Row.Cells[columnIndex].Attributes["onclick"] = js;
// Add a cursor style to the cells
e.Row.Cells[columnIndex].Attributes["style"] += "cursor:pointer;cursor:hand;";
}
}
}
}
Try to assign white color to the every cell and then overwrite according to conditions.
You need to use string.IsNullOrEmpty() like
if(string.IsNullOrEmpty(e.Row.Cells[i].Text))
{
e.Row.Cells[i].BackColor = Color.Blue;
e.Row.Cells[i].ForeColor = Color.White;
}
MSDN
Hope it works.

Creating a header row with buttons in a Custom GridView

After posting this: Custom Header in GridView
...I have a related problem. I have added the table row during OnDataBound, and it shows up, the links are clickable. There are two problems with adding it here: first, if a postback occurs that doesn't DataBind, the row disappears; second, no events are happening when the LinkButtons are clicked. Here is the OnDataBound code:
protected override void OnDataBound(EventArgs e)
{
base.OnDataBound(e);
// Hook up the handler to create the Selection header/footer
// TODO: Wrap this in a function sometime
Table table = (Table)Controls[0];
GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
// TODO: should add css classes
TableHeaderCell cell = new TableHeaderCell();
cell.ColumnSpan = Columns.Count + 1; // plus 1 for the checkbox column
cell.HorizontalAlign = HorizontalAlign.Left; // do this or css?
HtmlGenericControl label = new HtmlGenericControl("label");
label.InnerText = "Select:";
selectNoneLK = new LinkButton();
selectNoneLK.ID = "SelectNoneLK";
selectNoneLK.Text = "None";
selectNoneLK.Click += SelectNoneLK_Click;
//selectNoneLK.CommandName = "SelectNone";
//selectNoneLK.Command += SelectNoneLK_Click;
selectAllLK = new LinkButton();
selectAllLK.ID = "SelectAllLK";
selectAllLK.Text = "All on this page";
//selectAllLK.CommandName = "SelectAll";
//selectAllLK.Command += SelectAllLK_Click;
selectAllLK.Click += SelectAllLK_Click;
cell.Controls.Add(label);
cell.Controls.Add(selectNoneLK);
cell.Controls.Add(selectAllLK);
row.Controls.Add(cell);
// Find out where to put this row
int rowIndex = 0;
if(SelectionMode == SelectionMode.AboveHeader)
{
rowIndex = 0;
}
else if(SelectionMode == SelectionMode.BelowHeader)
{
rowIndex = 1;
}
else if(SelectionMode == SelectionMode.AboveFooter)
{
rowIndex = table.Rows.Count;
}
else if(SelectionMode == SelectionMode.BelowFooter)
{
rowIndex = table.Rows.Count + 1;
}
table.Rows.AddAt(rowIndex, row);
}
You can try putting it in the RowCreated Event, while the header is being created. This might also fix your problem with the LinkButtons not working.
void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.Header)
{
...your code here
}

Categories

Resources