C# gridview row onclick - c#

I notice there is a "AutoGenerateSelectionButton" function but it's not really what I want. I want to be able to two things when a row is clicked (anywhere of the row):
change the color of that entire row.
get the value of a specific column and update another table accordingly.
How can I acheive that without writing client-side javascript functions?

Assuming this is a webform, you need to access SelectedIndexChanged event of the gridview using a codebehind file.
from here you can modify properties
protected void ChangedRow(object sender, EventArgs e)
{
this.GridView1.SelectedRow.BackColor = System.Drawing.Color.Red;
....
}

Related

Add a column in gridview at run time

I want to add a new column in grid view at run time and in this column I want to add a button in some specific rows. How can I do this?
Please suggest me some relevant solution as soon as possible.
Although you can programmatically add column fields to the Columns collection, it is easier to list the column fields declaratively in the GridView control and then use the Visible property of each column field to show or hide each column field.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.columns(v=vs.100).aspx
So you see what you should do is go ahead and create the column in your GridView, but set the Visible property to False. Then to control the display of the column, you can make use of the GridView.RowDataBound Event.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound(v=vs.100).aspx
You should also be able to handle displaying a button from within the same RowDataBound handler.
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(condition)
{
Button btn = (Button)e.Row.FindControl("ButtonID");
btn.Visible = false;
}
}
}

How to select a WinForm DataGridView value, for a specific column, when anywhere along the entire row is clicked?

I've got a simple WinForm app which has a DataGridView on it. When the user clicks -anywhere- along the row results, I'm hoping to retrieve the value of the first column (i'll hard code that) which happens to be the ID of that row.
Anyone have any suggestions to how I can do this?
Lets assume the ID of the gridview is DataGridView1 <-- Bonus points for using the autoegenerated Id's :P
private void DataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
var value= DataGridView1[ID_ColumnName.Index,e.RowIndex].Value
}
You can also use CellMouseClick event handler and use the same code.
I hope you know how to get the event handlers.

Datagridview execute code on change of the column combo box value

I am trying to reload the datagridview on change of the combobox value in on of that datagridview columns. I know how to do the reload, but I am having trouble triggering the action. Should I be looking for value change in this particular cell or is there a change in comobobox action?
I typically use ComboBox_TextChanged events and you can use those within a datagridview as well, as you add them:
comboBox1.TextChanged += delegate(object sender, EventArgs e)
{
// Do Whatever
}
Do a DataGridView1.Refresh() on the combobox change event.

Command behind a GridView ButtonField

I got a question about the command behind a ButtonField (image type) in a GridView.
Got a GridView called gvIngevuld in wich I show rows of data and 1 ButtonField row.
Now I have to put code behind these buttons (Each of them the same) to put some things in PDF format.
The problem is I don't know how to put code behind these buttons ?
The code I have :
<asp:ButtonField ButtonType="Image" ImageUrl="~/Images/pdf.png" CommandName="pdf_click" />
As you can see I used the CommandName="pdf_click" property but when I click one of the buttons there's only a postback but nothing happens.
Anyone who can help me out here ?
in case needed, code behind :
protected void pdf_click(object sender, EventArgs e)
{
lblWelkom.Text = "Succes!";
}
Thanks.
You should use RowCommand event of gridview. Set the commandArgument to your item's unique key. (By default, it passes the row's index)
void gvIngevuld_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="pdf_click")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = gvIngevuld.Rows[index];
//gbIngevuld is your GridView's name
// Now you have access to the gridviewrow.
}
}
Also, If you have set the DataKeyNames of the gridview, you can access it as follows:
int index = Convert.ToInt32(e.CommandArgument);
int ServerID = Convert.ToInt32(GridView1.DataKeys[index].Value);
Just an addition, I wrote and tried exactly the same however the button still not working...
...it took some time till I realise that I forgot a thing, which was
OnRowCommand="<YourGridViewName>_RowCommand"
inside your GridView element in the **.cs* file
The CommandName property does not define the method to call. For that you need to create a method that is bound to the command event of the control.
I don't know too much about about GridViews i'm afraid but I would think the easiest way to do this would be to select it in design view in Visual Studio, go to properties, click on the events button (looks like a lightning flash) and double click on the RowCommand event propety. This should automatically create a method that is bound to a command button event.
You can then use the GridViewCommandEventArgs parameter to access the CommandName, CommandArgument and CommandSource properties to work out which buttom caused the event to fire.

How to retrive a Control in a specific ROW in edit mode in a GridView

I have a GridView with many rows.
When a User click the EDIT button in GridView I need to retrieve a Control in that specific row (now in edit mode).
This Logic should work on GridEvent _RowUpdating
At the moment my code (wrong) look inside every Row, so the Control founded is not unique and I receive an error.
// Event handler
protected void uxManageSponsoredContentsDisplayer_RowUpdating(object sender, GridViewUpdateEventArgs e)
// My code (Wrong!!!!):
foreach (GridViewRow row in uxManageSponsoredContentsDisplayer.Rows)
{
TextBox uxStartDate = (TextBox)row.FindControl("uxEffectiveStartDateInput");
}
Hope my question is clear. Any idea how to do it? Thanks
Solution:
TextBox uxStartDate = (TextBox)uxManageSponsoredContentsDisplayer.Rows[e.RowIndex].FindControl("uxEffectiveStartDateInput");
You need to use the GridViewUpdateEventArgs e as it contains index of row being updated.
Use something like
uxManageSponsoredContentsDisplayer.Rows[e.RowIndex].FindControl("uxEffectiveStartDateInput")

Categories