Clickable row in GridView and edit the row - c#

I have a problem here regarding to editable gridview. what I want to do is replacing edit button function by using a single clickable row. When I click a row, it should be forwarding me to a new page for editing those row data. How can I achieve this, without using edit button?
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
// only apply changes if its DataRow
if (e.Row.RowType == DataControlRowType.DataRow)
{
// when mouse is over the row, save original color to new attribute, and change it to highlight yellow color
e.Row.Attributes.Add("onmouseover",
"this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#EEFF00'");
// when mouse leaves the row, change the bg color to its original value
e.Row.Attributes.Add("onmouseout",
"this.style.backgroundColor=this.originalstyle;");
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";
//e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "CategoryID") + "'";
e.Row.Attributes.Add("style", "cursor:pointer;");
}
}

Simply add controls as you want to Default.aspx and on default.aspx pageload event(In not postback condition) retrieve the record through ID and populate the controls.
Now when submit button pressed update the record and redirect back to your original page

Related

How to display Tooltip on GridView Row Hover

I need to show a tooltip when mouse is placed on GridView Row (onmouseover)
I need to set the Tooltip content Dynamically in GridView_RowData
How can I Do this??
Can I Do this in e.Row.Attributes.Add(... ??
Try it like this...
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//use this way
e.Row.ToolTip = "My FooBar tooltip";
//or use this way
e.Row.Attributes.Add("title", "My FooBar tooltip");
}
}
This will show tooltip for entire row..If you need to show on a particular control then find that control and set is Tooltip attribute to your own title...
Can be Done like this. Here is the working copy.
What you need to do is, you have to find the control(for which you want to display tooltip on hover of mouse) inside the Gridview OnRowDataBound event and assign the tooltip text to the control.
protected void GridDepartment_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label LabelCoachName = e.Row.FindControl("LabelCoachName") as Label;
LabelCoachName.ToolTip = LabelCoachName.Text;
}
}
try this
If e.Row.RowType = DataControlRowType.DataRow Then
'your dynamic data fill to e.row.tooltip
e.Row.ToolTip = e.Row.Cells(1).Text & "-" & e.Row.Cells(3).Text
End If

ModalPopupExtender open onclick of GridView row problems

(I'm using Lukinha RS's solution to the row onclick functionality)
When I click on a row within the gridview I get a postback before the ModalPopupExtender opens, I dont want the postback however as you see the method I use is the cause. Unfortunatly it is the only way I have been able to get an onClick applied to a gridview row to open the MPE.
Another problem I have is with the MPE open - I click a 'close' button on the popup panal it simply reloads the page resulting with the same popup panal opening.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.DataItem == null)
{
return;
}
try
{
switch (e.Row.RowType)
{
case DataControlRowType.Header:
break;
case DataControlRowType.DataRow:
e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand'");
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" + e.Row.RowIndex.ToString()));
break;
}
}
catch
{
return;
}
And here is my SelectedIndexChanged
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = ((GridView)sender).SelectedRow;
ModalPopupExtender mpe = (ModalPopupExtender)row.FindControl("ModalPopupExtender1");
mpe.Show();
}
Unfortunatly it is the only way I have been able to get an onClick
applied to a gridview row to open the MPE
Incorrectly. Actually, you CAN open modal extender without postback.
Change onclick attribute value as below:
e.Row.Attributes.Add("onclick", String.Format("javascript:$find('{0}').show();", ModalPopupExtender1.ClientID));

How to change the color of label depending on certain values in the gridview

I have a sqldatasource and gridview.
I have two columns:
ID | Status
1 Closed
2 Opened
3 Waiting
How to change the color of the label in the view state of the gridview depeding on the value from the 'status' column.
For example if the value in the cell is "Closed" the label color will be red , if it's opened then it will become green and so on.
I've thought about looping through all cells of the status column and if the cell contains a certain value the color will change. ( in row data bound event ) . But I haven't done this because I don't find this idea as a good one because of the looping part.
use the RowDataBound event from the grid view to check what is the status. You don't need to do a loop because that event (if you register or not) is being called.
One thing to note, you will need to make sure you are looking at the right row (header, footer, alternate etc) so something like this
void YourGridViewName_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Do your color change here by accessing the col with e.Row.Cells[column_index].BackColor = 'what ever you want'
}
}
You can enable the RowDataBound Event in the markup
<asp:GridView ID="gridview1" runat="server" OnRowDataBound="RowDataBound">
</asp:GridView>
And put this in your Code-Behind file.
protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the underlying data item. In this example
// the underlying data item is a DataRowView object.
DataRowView rowView = (DataRowView)e.Row.DataItem;
// Retrieve the state value for the current row.
String state = rowView["state"].ToString();
//format color of the as below
if(state == "Closed")
(e.Row.FindControl("lbl1") as Label).BackColor = Color.Red;
if(state == "Open")
(e.Row.FindControl("lbl1") as Label).BackColor = Color.Green;
if(state == "Waiting")
(e.Row.FindControl("lbl1") as Label).BackColor = Color.Yellow;
}
}
You have to write code in the rowdatabound event of your grid view.
Example:
private GridView1_RowDatabound(object sender,EventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// You have to put your logic here.
if( e.Row.Cells[1].Text == "closed" )
{
// to get a reference to label control
Label lb = e.Row.FindControl("LabelCOntrolID");
}
}
}

Is it Possible to Change the text color for columns in Grid View based upon search item?

Note: i need to change in different columns based upon search request.
Yes you can do it in GridView.RowDataBound event
Like this
protected void yourGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].BackColor = Color.Beige;
// 0 could be any valid cell index in your row
}
}
for more info Go here

Selecting an Item in GridView with ITemplate Generated Button

I'm having trouble figuring out how could I use my imagebutton (see below) in my ITemplate to append the button's corresponding row data (ItemID) as a query string.
My ImageButton in my ITemplate:
ImageButton select_button = new ImageButton();
select_button.ID = "select_button";
select_button.ImageUrl = "~/Files/System/Icons/highlighter.png";
select_button.CommandName = "Select";
select_button.ToolTip = "Select";
container.Controls.Add(select_button);
Should I handle it in in the imagebutton's OnClick event (if so, is there a way to get the row where the button is located) or can I handle in in the GridView events (rowbinding, rowseleted, rowcommand, etc.)?
I'd be glad to elaborate more on my code upon request. ^ ^
You can set the ID in the CommandArgument property of your button control in the RowDataBound Event. Once you have an ID, you can track rows with it.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow dr = ((DataRowView)e.Row.DataItem).Row;
((Button)e.Row.FindControl("select_button")).CommandArgument = dr["IdColumn"].ToString();
}
}

Categories