I am a new ASP.NET developer. I am using a DetailsView now to dispaly some data from the database. I have the need to highlight certain two rows from the DetailsView. Both rows are VARCHAR data type.
SO HOW TO DO THAT?
Override the databound event and set the e.Row.BackColor = System.Drawing.Color.Red; or what ever color, if you have some logic to execute for finding the row that needs to be highlighted.
protected void detailsView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(...)//some condition for selection of row to be higlighted
{
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
If you need a perticular row, say 4th row to be highlighted and that's fixed then you could directly like this
detailsViewGrid.Rows[3].Row.BackColor = System.Drawing.Color.Red;
The only thing you need to keep in mind that this code would be written in only those events which would come after grid_rowdatabound event(Like pre-render)
Related
I am working on a gridview in ASP.Net. I want a particular row to be highlighted on checkbox check so that if any other user logs in the system the row should appear highlighted to them too.
I would suggest create a flag field in your Database table that you bind with the grid view. If you check one row then pass true value to the flag field. Then while you show the Grid view validate like this,
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "IsChecked")) == "True")
{
e.Row.BackColor = System.Drawing.Color.Yellow;
}
else
{
e.Row.ForeColor = System.Drawing.Color.Blue;
}
}
}
I have an SQL Database which contains hex codes for colors. I want the change every single row of the grid according to the hex code. One single line contains(On DB) Team, Year, City, Stadium, Fore_Color, Back_Color. I tried using Eval to get the specified color for each line but couldn't manage to do it. I am using c#, asp.net.
For Example why this does not work?:
e.Row.BackColor = Color.FromKnownColor(System.Drawing.ColorTranslator.FromHtml(DataBinder.Eval(e.Row.BackColor,"Back_Color")));
OR THIS:
e.Row.BackColor = ColorTranslator.FromHtml(Eval("Back_Color").ToString());
e.Row.ForeColor = ColorTranslator.FromHtml(Eval("Front_Color").ToString());
This gives the following error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
I tried saying OnRowDataBound for gridview but it didn't work.
Use the RowDataBound event of the GridView and ColorTranslator.FromHtml:
protected void GrdidView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;
Color backColor = ColorTranslator.FromHtml(row.Field<string>("Back_Color"));
e.Row.BackColor = backColor;
Color frontColor = ColorTranslator.FromHtml(row.Field<string>("Front_Color"));
e.Row.ForeColor = frontColor;
}
}
If you are using a different DataItem look at it in the debugger and change it accordingly.
I have a sqldatasource and gridview.
I have two columns:
ID | Status
1 Closed
2 Opened
3 Waiting
How to change the color of the label in the view state of the gridview depeding on the value from the 'status' column.
For example if the value in the cell is "Closed" the label color will be red , if it's opened then it will become green and so on.
I've thought about looping through all cells of the status column and if the cell contains a certain value the color will change. ( in row data bound event ) . But I haven't done this because I don't find this idea as a good one because of the looping part.
use the RowDataBound event from the grid view to check what is the status. You don't need to do a loop because that event (if you register or not) is being called.
One thing to note, you will need to make sure you are looking at the right row (header, footer, alternate etc) so something like this
void YourGridViewName_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Do your color change here by accessing the col with e.Row.Cells[column_index].BackColor = 'what ever you want'
}
}
You can enable the RowDataBound Event in the markup
<asp:GridView ID="gridview1" runat="server" OnRowDataBound="RowDataBound">
</asp:GridView>
And put this in your Code-Behind file.
protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the underlying data item. In this example
// the underlying data item is a DataRowView object.
DataRowView rowView = (DataRowView)e.Row.DataItem;
// Retrieve the state value for the current row.
String state = rowView["state"].ToString();
//format color of the as below
if(state == "Closed")
(e.Row.FindControl("lbl1") as Label).BackColor = Color.Red;
if(state == "Open")
(e.Row.FindControl("lbl1") as Label).BackColor = Color.Green;
if(state == "Waiting")
(e.Row.FindControl("lbl1") as Label).BackColor = Color.Yellow;
}
}
You have to write code in the rowdatabound event of your grid view.
Example:
private GridView1_RowDatabound(object sender,EventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// You have to put your logic here.
if( e.Row.Cells[1].Text == "closed" )
{
// to get a reference to label control
Label lb = e.Row.FindControl("LabelCOntrolID");
}
}
}
Note: i need to change in different columns based upon search request.
Yes you can do it in GridView.RowDataBound event
Like this
protected void yourGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].BackColor = Color.Beige;
// 0 could be any valid cell index in your row
}
}
for more info Go here
I am currently working on a grid view. I am using the allow paging method which is working fine and positioned to the right of the grid view.
I want to hide the first column which is working fine except it also removing the paging numbers which stops the user from being able to change page numbers.
Below is the code I am using to hide the column
protected void tblLog_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
}
The above code hides the correct column but also hides the automatic page numbers created by the grid view allowpaging method.
Thanks for any help you can provide.
Check first to see if it's actually a data row:
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Visible = false;
}
You need to add a condition for datarow and dataheader both
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Visible = false;
}