Changing the style of specific records in GridView - asp.net

I'm designing a logistics system in ASP.Net . In the Order processing page, orders are displayed by Grid View,i want to change the font style of the rows to BOLD, which are marked as"ordedr not processed". thanks.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string OrStatus = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Orderstatus"));
if (OrStatus == "Order not processed")
{
//You can use whatever you want to play with rows
e.Row.Cells[0].Font.Bold = true;
e.Row.Cells[2].CssClass = "gridcss";
}
}
}
Follow that code. It will helps

You can do this in "rowdatabound" event of grid.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView grid = GridView1;
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
string orderstatus= Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Orderstatus"));
if(orderstatus=="Order not processed)
{
//write your code to change css
}
}
}

Related

Enable/disable a CheckBox column on edit/update

I have grid with a CheckBox column like this:
I want to simply enable the CheckBox of selected row "e" on Edit and disable after Update/Cancel. This is what I have tried:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((CheckBox)e.Row.FindControl("chkStatus")).Enabled = false;
}
}
catch
{
}
}
This is for enabling the CheckBox on Update/Edit:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gvRow = GridView1.Rows[index];
if (e.CommandName == "Edit" || e.CommandName == "Update")
{
((CheckBox)gvRow.FindControl("chkStatus")).Enabled = true;
}
else if (e.CommandName == "Cancel")
{
}
}
But the problem is that after each Edit/Update, RowDataBound fires and disables the CheckBox again.
How can I avoid this?
OK, found the solution.
Just needed to change to:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState != DataControlRowState.Edit)
{
// Here logic to apply only on initial DataBinding...
}
}

change background color of a specific Row asp.net c#

I need a hand.
How to change background color of a entire Row on dataBound Event Programatically in a gridview ?
protected void databound_gridview(object sender, GridViewRowEventArgs e)
{
}
On top of my head you'll need to create a RowDataBound Event and then you can do something like
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == 1)
{
e.Row.BackColor = Color.Red;
}
}

HTML tags in Content

I'm implementing full text search on a table named as tbljobs on the cloumn named as jobdescription. In front end I'm getting html tags in the description. I'm showing the records in GridView and on Gridview's RowDataBound enent I'm decoding the text. I'm using the following code on Gridview's RowDataBound event:
protected void GridNewlyPostedJobs_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string decodedText = HttpUtility.HtmlDecode(((Label)e.Row.FindControl("lblJobDescription")).Text);
((Label)e.Row.FindControl("lblJobDescription")).Text = decodedText;
}
}
But nothing works..!!
Use DataRowView to get the Data...
protected void GridNewlyPostedJobs_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView rowView = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
string decodedText = HttpUtility.HtmlDecode(rowView["jobdescription"]);
((Label)e.Row.FindControl("lblJobDescription")).Text = decodedText;
}
}

How do I get the value of a gridview Cell?

How can I get the value of a gridview cell? I have been trying the code below with no luck.
protected void grvExpirations_RowDataBound(object sender, GridViewRowEventArgs e) {
int test = Convert.toInt32(e.Row.Cells[5].text;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if(e.Row.RowType == DataControlRowType.DataRow)
{
string LinkText = (string)System.Web.UI.DataBinder.Eval(e.Row.DataItem, "RegistrationLinkText");
if(LinkText == "text")
{
e.Row.Cells[3].Text = "your text";
}
}
}
protected void grvExpirations_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int test = Convert.toInt32(e.Row.Cells[5].Text);
}
}
use this code below,
protected void grvExpirations_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int test = Convert.ToInt32(Server.HtmlDecode(e.Row.Cells[5].Text.Trim()));
}
}
and please remember cell index number starts from 0
If you use the above methods you will get the value of cell[5] for the last row only.
So if you are specific about a certain row I think you can get the value from any other gridview event handlers.

How to use ColumnName in GridView control to hide some columns

I want to hide few columns of a gridview before they gets displayed.
I want to do it by create a common function which can be used by multiple controls.
I am using an extension and would like to know how it can be done.
Here is my code
protected void btnStandardView_Click(object sender, EventArgs e)
{
_viewTypeDl = new ViewTypeDL();
DataTable dt = _viewTypeDl.GetStandardView();
gvViewType.Source(_viewTypeDl.GetStandardView(),"ColorCode");
ViewState["request"] = "Standard View";
}
public static void Source(this CompositeDataBoundControl ctrl, DataTable dt, params string[] ColumnsToHide)
{
ctrl.DataSource = dt;
ctrl.DataBound += new GridViewRowEventHandler(ctrl_DataBound);
ctrl.DataBind();
}
static void ctrl_DataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells["ColorCode"].Visible = false;
}
I want to create an extension to hide or show columns provided in the list as an array.
1st function is used on page. While below two functions are needs to be used for multiple applications
There are two ways you can meet your requirement.
set gvViewType.Columns[i].visble = false;
Allow css to handle the hidden columns for you.
.hidden
{
display:none;
}
.visble
{
display:block;
}
//This is the Gridview event.
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Cells Represent the Column
e.Row.Cells[0].CssClass = "hidden";
}
else if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].CssClass = "hidden";
}
}

Resources