String manipulation in GridView on render (or prerender) - asp.net

I need to dynamically modify the contents of a column in a GridView1 before it is displayed. Basically, I need to convert every 'Environment.NewLine' in a field to a so it displays as a new line on an ASP.NET page.
How do I do this?

You can use the RowCreated event to alter rows as they have been created and then edit specific columns. I think to get the columns you use
void ProductsGridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
e.Row.Cells[1] += "<br />"
}

I assume you're using a data source to bind this grid. If so, I'd suggest the RowDataBound event. I doubt you'd have any newlines in your header row, but there isn't much sense in checking or appending HTML to them.
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
e.Row.Cells[i].Text.Replace(Environment.NewLine, "<br />");
}
}
}

Related

Loop through a gridview to replace the cell value

I want to loop through a gridview cell values and replace any value which has an asterisk like 3*,4*, ** etc to change like 3(underline), 4 underline and 8 (underline)..so basically i want to remove the asterisk and underline the inetger..Please guide me on this...thank you
You can loop trough every row and cell trough this:
foreach(DataGridViewRow gridRow in myGridview.Rows)
{
for(int i = 0; i < myGridview.Columns.Count; i++)
{
if(gridRow.Cells[i].Text.Contains('*'))
{
//Do your thing
gridRow.Cells[i].Text=gridRow.Cells[i].Text.Replace(#"*", "");
gridRow.Cells[i].Style.Font = new Font("Ariel", 8, FontStyle.Underline);
}
}
}
You can achieved this by using RowDataBound Event.. No need to loop
GridView.RowDataBound Event
The following example demonstrates how to use the RowDataBound event to modify the value of a field in the data source before it is displayed in a GridView control.
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
}
}

Add tooltip to only some cells of Grid View

I need to add a tooltip to a TemplateField in a GridView, but only to Row that meets some condition. How can I achieve that?
protected void GridView1_DataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// check for condition
if (e.Row.Cells[0].Text.Contains("sometext"))
{
e.Row.ToolTip = "Text to be shown on mouseover of the row";
}
}
}
Use the row databound which will hit each row as it is created. Then inside use .findcontrol to get whatever control it is that you are adding a tooltip to.
Then assign the tooltip.
Wrap this in whatever condition you want to use.
If you post your current code then I may be able to help further.
try this,
This event through you can check your all gridview rows.
gridview row event Name is =OnRowDataBound : "GridView1_RowDataBound"
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Add Your Condition here
}
}

Disable GridViewColoum on Page_Load

I have a Delete-Button in my gridview that i used to create with an TemplateField + LinkButton + OnRowCommand.
Now a normal user should not be able to use this button - or better not to see this button at all.
How to disable a coloumn in a gridView on the page Log event?
Try this:
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// get the column here and your condition to make that disabled
e.Row.Cells[index].Visible = false;
}
}
You can also hide like:
((DataControlField)gridView.Columns
.Cast<DataControlField>()
.Where(fld => (fld.HeaderText == "Title"))
.SingleOrDefault()).Visible = false;
Use this: column visible use before bind grid otherwise error occurring.
protected void Page_Load(object sender, EventArgs e)
{
gridView.DataSource = "yourDatasource";
gridView.DataBind();
gridView.Columns[ColumnIndex].Visible =false;
}
Try this
In your Page_Load
GridView1.Columns[0].Visible = false;
then column 0 of the grid become disable and the other column of the grid resize automatically.

Change Field to Checkbox inside GridView

I am trying to change fields to a Checkbox inside a GridView.
I currently create Grid Columns Dynamically based on a query and some of the columns I want to change to a checkbox so it can be checked/unchecked by the user. I know I cant do this via .aspx page using but I am trying to stay away from statically creating the fields.
Any help would be great.
Make use of GridView's RowDataBound Event. Thus you can add any control to your GridView.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk1 = new CheckBox();
chk1.ID = "chkbox1";
e.Row.Cells[0].Controls.Add(chk1);
}
}
Edit for Comment:
Once you passed the values from database to gridview (out of scope for this question), You can access the values using e.Row.Cells[i].Text where "i" is the row.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txt1 = new TextBox();
txt1.Text = e.Row.Cells[0].Text;
e.Row.Cells[0].Controls.Add(txt1);
}
}

ChildGridView column's Data On Binding

i am using nested gridview, on RowDatabound Event i am binding childgridview based on some condition of parentgridview column. meanwhile i need to know the column value of child gridview.how can i do that?
I am using this code:-
protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gridViewchild = (GridView)e.Row.FindControl("gridView2");
string custState = ((DataRowView)e.Row.DataItem)["CustState"].ToString();
if (!string.IsNullOrEmpty(custState))
{
gridViewchild .DataSource = MyDataSource;
gridViewchild .DataBind();
//now i need to know the childgridview column's data ????
}
}
}
There are couple of ways you can achieve this:
1- Loop through the rows of Child GridView
foreach (GridViewRow row in childGridView.Rows)
{
//row.Cells[index];
}
2- Use RowDataBound event of child GridView

Resources