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
Related
Its a weird requirement but, Can I alter values of a single column of a databound gridview and display it in the same gridview ? Say, in GridView_RowDataBound(object sender, GridViewRowEventArgs e). I have edited the code for what I am actually doing. Problem is "DataItem" is a typeof class(entity fetched db table) and converting it to Datarow is not possible. So how do i go about it?
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (drv["Id"] != DBNull.Value)
{
string val = CommonUtil.Decrypt(drv["Id"].ToString());
e.Row.Cells[3].Text = val ;
}
}
}
Yes it's doable in GridView RowDataBound something like:
if(e.Row.RowType==DataControlRowType.DataRow)
{
//say you want to set value of 3rd column to "Hello"
// e.Row.Cells[2].Text="Hello"; //0 based index
MyCustomClass myitem= (MyCustomClass) e.Row.DataItem;
if (myitem.Id != null)
{
string val = CommonUtil.Decrypt(myitem.Id.ToString());
e.Row.Cells[3].Text = val ;
}
}
If that doesn't help, please share some code and more information on exactly what column you want to change and to what value.
You can cast to the type you want:
MyCustomClass drv = (MyCustomClass) e.Row.DataItem;
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
}
}
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.
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);
}
}
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 />");
}
}
}