Event to bind a gridview inside another gridview - c#

I have a gridview control inside another gridview control. When I click on the edit button the second gridview inside the 1st one should bind with a data source.
I am using a SqlDataSource and configured it. In which event of the gridview do I need to write the code for binding the records?
I am new to .net.

You can use RowCommand Event
protected void SecondGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
FirstGrid.DataBind()
}
}

You need to write your data binding logic in Parent grid's OnEditCommand event. Further details can be found here

Related

How to remove the focused row when a gridview is initially loaded

I have a DevExpress gridview that loads on my screen. Initially, I do not want it to have a focus on any row. Only after a user selects a row do I want there to be focus. Is there a way to achieve this?
if you have multiple rows selection option enabled on your grid you can call the
gridView1.ClearSelection();
method on the gridview. However if it is not enables you have to call
gridView1.UnselectRow(5);
method. You can read all such methods here
with Mvvm pattern you can try this.
I had the same problem and have solved it by overriding the OnPropertyChanged event of the GridView
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) { base.OnPropertyChanged(e); this.ClearSelection(); }

C# gridview row onclick

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

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")

Datagrid button column how to bind a function to a button?

Currently i have a datagrid view that displays names in the 1st columnnd in the 2nd column delete buttons.
This datagrid view already has bind data to a function so it can displays the names that are withint a xml file (first column is a hyperlink column).
But next i want to be able to delete the xml values that are in the first column.
(by clicking on the delete button on the second column of it)
<name></name>
But how do i exactly bind a (delete) function to these buttons?
name1 btnDelete
name2 btnDelete
etc...
Thanks in advance.
For datagrid you have handle the ondeletecommand event, see below.
<asp:DataGrid runat="server" ID="dg" ondeletecommand="dg_DeleteCommand">
then define the event handler in your code behind .. see below.
protected void dg_DeleteCommand(object source, DataGridCommandEventArgs e)
{
}
the DataGridCommandEventArgs has all the properties you'll need to check including e.CommandName etc
Hope this helps.
I don't know about datagrid but in gridView I use
set button's command name and use GridView's RowCommand Event.
if(e.commandName == "delete")
//ur code;
Template field
for default delete of GridView use
girdViewRowDeleting
Hope it will help..

Categories