Row background color in gridview? - asp.net

In asp.net application, I am using grid-view control in that I am binding the data to the label which is in grid-view.
If data is empty then the color of the row should be in red
If not I mean if data is there to bind then the row in green.
This is my code:
<asp:TemplateField HeaderText ="Holiday Region">
<ItemTemplate >
<asp:Label ID ="lblholdareg" runat ="server" Text ='<%# Eval("Holidaregion") %>' >
</asp:Label>
</ItemTemplate>
</asp:TemplateField>

You need to handle the RowDataBound event, get into the e.Row item, and assign either a CSS class or directly set the background color. I prefer setting a CSS class so you can change the rendering of it without a recompile later.
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Holiday Region">
<ItemTemplate>
<asp:Label ID="lblholdareg" runat="server" Text='<%# Eval("Holidaregion") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And the code-behind, I had to assume you were using a DataTable as your data source, update the code to fit your data structure:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
System.Data.DataRow row = (System.Data.DataRow)e.Row.DataItem;
if (row["Holidaregion"] == null || row["Holidaregion"].ToString().Trim().Length == 0)
{
e.Row.CssClass = "row-empty";
}
else
{
e.Row.CssClass = "row-full";
}
}

You can do it on rowdatabound function of gridview as follows
protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//change it according your cell number or find element
if(e.Row.Cells[0].Text != "")
e.Row.BackColor = Color.Green;
else
e.Row.BackColor = Color.Red;
}
}

if(e.Row.RowType == DataControlRowType.DataRow)
{
Control l = e.Row.FindControl("Label1");
((Label)l).BackColor = System.Drawing.Color.Red;
}

Try something like this
<asp:TemplateField HeaderText ="Holiday Region">
<ItemTemplate >
<asp:Label ID ="lblholdareg" runat ="server"
CSSClass='<%# (String.IsNullOrEmply(Eval("Holidaregion")))?"red:green" %>'
Text ='<%# Eval("Holidaregion") %>' >
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Edit:
Instead of fighting with grid view inline and code behind stuffs, just use a jQuery and achieve the same in client side

try it this code it is every row in color change with the category wise or filters wise
http://devloper4u.blogspot.in/2013/10/how-to-set-color-in-every-row-on.html

Related

Gridview taking more time to bind

I am fetching data from stored procedure and showing in the grid view.
My sql query takes max 1 sec to execute but while binding to gridview it takes more time .
In my page only one gridview is present. and the load time of my page is 10 sec :(
so how to increase my loading time.(i searched on google and didn't find any good solution :( )
My Gridview
<asp:GridView ID="gvStatusStatistics" runat="server" AutoGenerateColumns="False"
CssClass="grid" OnRowCreated="gvStatusStatistics_RowCreated">
<Columns>
<asp:TemplateField HeaderText="PNSC_Leads_LeadStatus">
<ItemTemplate>
<asp:HyperLink ID="hlLeadStatus" ForeColor="#000000" runat="server" Text='<%#Eval("TranslatedStatus") %>'
NavigateUrl='<%# string.Concat("~/ViewLeads.aspx?LeadStatusDefinition=", Eval("Status")) %>'
ToolTip="" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PNSC_Leads_NumberOfLeads">
<ItemTemplate>
<asp:HyperLink ID="hlLeadsCount" ForeColor="#000000" runat="server" Text='<%#Eval("Value") %>'
NavigateUrl='<%# string.Concat("~/ViewLeads.aspx?LeadStatusDefinition=", Eval("Status")) %>'
ToolTip="" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void gvStatusStatistics_RowCreated(object sender, GridViewRowEventArgs e)
{
Trace.Write("gvStatusStatistics_RowCreated", "Start");
if (e.Row.RowType == DataControlRowType.DataRow)
{
int CellCounter = 0;
foreach (TableCell cell in e.Row.Cells)
{
CellCounter++;
// track which row this is
if (CellCounter % 2 == 0)
{
// change the alternating one background color
cell.CssClass = GridCellAlternating1Css;
}
else
{
// change the alternating zero background color
cell.CssClass = GridCellAlternating0Css;
}
}
}
}
Please help to solve this.
Thanks
below line taking more time
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
{
}

How to set disable or readonly in a datagrid view

I have the following datagrid view that is data bound with some data at the DB
I need to load the information in the data grid and make all the textbox in some columns(in the example only comes on but there are many) to be readonly or disable.
<asp:DataGrid ID="grdRequestTypeItem" TabIndex="1" runat="server" CssClass="Grid" AutoGenerateColumns="False"
AllowSorting="True" Visible="true">
<SelectedItemStyle CssClass="GridSelectedItem"></SelectedItemStyle>
<AlternatingItemStyle CssClass="GridAlternatingItem"></AlternatingItemStyle>
<ItemStyle CssClass="GridItem"></ItemStyle>
<HeaderStyle CssClass="GridHeader"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Lot Number">
<ItemTemplate >
<asp:TextBox ID="txtLot" runat="server" Width="100%" Text='<%# DataBinder.Eval(Container, "DataItem.Lot") %>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
How can I disable that column in the code behind ??
Neither of the following are working
grdRequestTypeItem.Columns[1].IsReadOnly = true;
((BoundField)grdRequestTypeItem.Columns[0]).ReadOnly = true;
((TemplateField)grdRequestTypeItem.Columns[0]).EditItemTemplate = null;
Even if your DataGrid is not in EditMode it displays the TextBox inside the ItemTemplate. Otherwise you use an EditItemTemplate. That's why neither of your solutions work. You have several options:
Put a <asp:Literal> Control into your ItemTemplate
Bind the ReadOnly property of the TextBox to a bool value of your viewModel
from code behind, you have to address the correct control
you could use something like this to reference the textbox within the item template
foreach (GridViewRow row in grdRequestTypeItem.Rows)
{
var txtLot = row.FindControl("txtLot") as TextBox;
txtLog.IsReadOnly = true;
}
You could also use the DataBinding Event on the row instead of looping through the rows like in the answer of #Stealth22
Try this:
<asp:DataGrid ID="grdRequestTypeItem" TabIndex="1" runat="server" CssClass="Grid" AutoGenerateColumns="False" AllowSorting="True" Visible="true"
OnItemDataBound="grdRequestTypeItem_ItemBound">
And then in your code-behind...
protected void grdRequestTypeItem_ItemBound(Object sender, DataGridItemEventArgs e)
{
if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
TextBox txtLot = e.Item.FindControl("txtLot");
if (txtLot != null)
{
txtLot.ReadOnly = true;
}
}
}
Someone can correct me if I am wrong anywhere, as I work more with GridViews than DataGrids, but as far as I know, the principle is the same.

ASP.Net DropDownList selected value missing

At the beginning I have to say that I tried to find the answer... And yes I'm new in ASP.Net world :)
I would like to use DropDownList in an EditItemTemplate field in GridView. I found I cannot set parametr SelectedValue. It's missing. When I try to set it in code behind it seems to ddlEditPermissions doesn't exists.
<asp:TemplateField HeaderText="opravneni" SortExpression="opravneni">
<edititemtemplate>
<asp:DropDownList ID="ddlEditPermissions" runat="server" DataSource='<%# getPermissions() %>' OnPreRender="ddlEditPermissions_PreRender"/>
</edititemtemplate>
<insertitemtemplate>
<asp:TextBox ID="tbEditPermissions" runat="server" Text='<%# Bind("opravneni") %>'></asp:TextBox>
</insertitemtemplate>
<itemtemplate>
<asp:Label ID="lEditPermissions" runat="server" Text='<%# Bind("opravneni") %>'></asp:Label>
</itemtemplate>
I'm really confused. Could anyone advise me?
You can use RowDataBound of the GridView which gets triggered for every GridViewRow after it was constructed and databound:
protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit)
{
var ddlEditPermissions = (DropDownList)e.Row.FindControl("ddlEditPermissions");
// bind DropDown manually
ddlEditPermissions.DataSource = getPermissions();
ddlEditPermissions.DataTextField = "Permission_Name"; // presumed text-column
ddlEditPermissions.DataValueField = "Permission_ID"; // presumed id-column
ddlEditPermissions.DataBind();
DataRowView dr = e.Row.DataItem as DataRowView; // you might need to change this type, use the debugger then to determine it
ddlEditPermissions.SelectedValue = dr["Permission_ID"].ToString(); // presumed foreign-key-column
}
}

MaskedEditExtender is not working from 2nd line in gridview in Asp.Net

I have a gridview on my page, there is three columns, one is for quantity, i need to use MaskEditExtender with simple TextBox, but it is working only on first row, from second row the textbox is appearing without mask.
here is my code :
<Columns>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:TextBox ID="txtQuan" runat="server" ValidationGroup="MKE" MaxLength="5"
style="text-align:right" width="100px" Text='<%# BIND("QUAN") %>' />
<ajaxToolkit:MaskedEditExtender ID="txtQuan_MaskedEditExtender" runat="server"
Enabled="True" Mask="99999" TargetControlID="txtQuan" />
</ItemTemplate>
</asp:TemplateField>
Reason is that When Gridview Render row each Row contain textbox with deffferent autogenerated id and you are given a fixed name in TargetControlID Property in MaskEditExtender . so you need to use RowDataBound Event to Put MaskEditExtender in each textbox....
using AjaxControlToolkit; protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txt = (TextBox)e.Row.FindControl("txtQuan");
MaskedEditExtender mxt = (MaskedEditExtender)e.Row.FindControl("MaskedEditExtender1");
mxt.TargetControlID = txt.ID;
}
}

GridView: convert data before bindind to GridView

My GridView using following data binding syntax to bind data.
I hope do data conversion before it is really bound to GridView. For example, if the VendorID int property (I use a List as data source) is 0, I wish showing empty string on that field.
What kind of event I could exploit? And if you could suggest any code sample?
Thanks a lot.
<asp:TemplateField HeaderText="Vendor ID">
<ItemStyle Width="1%" BorderColor="#efefef" BorderWidth="1px" />
<ItemTemplate>
<asp:Label runat="server" ID="lblNo" Text='<%# Bind("VendorID") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle BorderColor="#efefef" />
</asp:TemplateField>
Gridview's RowDataBound Event can be use for this.
void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// do wat ever u like to do here.
// formating, checking, changing data, etc....
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
}
}
hi you can use following code in RowDataboundEvent
if (DataBinder.Eval(args.Row.DataItem, "VendorID").ToString() == "0")
{
((Label)args.Row.FindControl("lblNo")).Text = "";
}
else
{
((Label)args.Row.FindControl("lblNo")).Text = DataBinder.Eval(args.Row.DataItem, "VendorID").ToString();
}

Resources