Add tooltip to only some cells of Grid View - asp.net

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
}
}

Related

Adding/using Attributes for ASP.Net controls

I was wondering where can we get a list of attributes that we can add to an ASP.NET control.
An example is the following block of code from :
protected void Grid1_OnItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
((CheckBox)item["TestColumn"].Controls[0]).Attributes.Add("onClick","javascript:hi();");
}
}
The following code couldn't work which I figure could be due to 'onClick' not applicable to Checkbox ASP.NET control.
Hence, is there a place we can refer to for attributes where we can add to each ASP.NET control? And also for the following code:
Attributes["click"] = abc();
Thanks.
Attributes must be added on RowCreated event of GridView. Please check below.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Dummy Code
Button btnButton =
(Button)e.Row.Cells[4].FindControl("btnButton");
btnButton.Attributes.Add("onmouseover",
"this.className='actionH'");
}
}

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.

Changing the style of specific records in GridView

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
}
}
}

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