How to select 1 Gridviews in a event? - c#

I have 2 gridviews which update some data, they are independent each one,
both are using "onrowupdating="actualizar"
i want to use the same event like below:
protected void actualizar(object sender, GridViewUpdateEventArgs
e)
{ }
How can i select the specific grid view inside the event?

The object sender will be the GridView who started the event so you can cast it to GridView and you will have it's properties like its Id.
Example:
(sender as GridView).ID

Thats the answer !!
Control cntrl = sender as GridView;
var gridId = cntrl.ID;

Related

trigger an eventhandler on postback c#

I am pretty new to c# programming.
I have an issue that follows:
I have a GridView on a web form, a DropDownList and a Label control. When i select a value from a DDL a Grid View is populated with rows from the database that equal the DDL condition(in my case DDL represent Countries, GV lists the Cities).
When i delete the last City from a GV using a built in GV Delete function i would like to automatically write in a Label that there are no more Cities in selected Country.
How can i achieve that?
I tried to put
protected void GridView1_RowDeleted1(object sender, GridViewDeletedEventArgs e)
{
if (GridView1.Rows.Count == 0)
{
LabelGrid.Text = "No more cities.";
}
}
but it didn't work.
Thanks for your help
Consider using PreRender event, when that event runs the Row.Count property contains the correct value. (decremented after delete)
protected void GridView1_PreRender(object sender, EventArgs e)
{
if (GridView1.Rows.Count == 0)
{
LabelGrid.Text = "No more cities.";
}
}

Catch dropdown selected text inside a repeater

I have dropdownlist inside a repeater and whenever the selected text is changed i have to show it in a textbox how can i do this??
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownList6");
TextBox txt = (TextBox)e.Item.FindControl("TextBox4");
txt.Text = ddl.SelectedItem.Text;
}
First, don't use ItemCreated therefore since it triggered too early in the life-cycle(for the ViewState). You would also have to check for the ItemType first.
Instead use the DropDownLists SelectedIndexChanged event directly:
protected void Ddl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList) sender;
RepeaterItem item = (RepeaterItem) ddl .NamingContainer;
TextBox txt = (TextBox) item.FindControl("TextBox4");
txt.Text = ddl.SelectedItem.Text;
}
you could add appropriate OnSelectedChange (somwthing) event handler to the DropDownList and then when event fired you catch it and do whatever you want , you can do it in both client side or server side .
You will need to use the add a handler to associate each dropdown control with the appropriate event handler. I don't have VS in front of me but it should be something like:
txt.SelectedIndexChanged += new EventHandler(YourMethodName)

c# dropdownlist selectedindexchanged in gridview sets selectedindex of second dropdownlist

I have a gridview with two dropdownlists populated from a db. One is a descriptive name, the other is an abbreviated name. I need to accomplish the following:
When I select an item in DDL1 I need to change the selected index of DDL2 to match, and vice versa.
I have searched on here and found the following:
protected void ddlAddLabTest_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlLabTest = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddlLabTest.NamingContainer;
DropDownList ddlAddLabTestShortName = (DropDownList)row.FindControl("ddlAddShortname");
ddlAddLabTestShortName.SelectedIndex = intSelectedIndex;
}
Only when it gets to the assignment for "row" I receive the following:
Unable to cast object of type 'System.Web.UI.WebControls.DataGridItem' to type 'System.Web.UI.WebControls.GridViewRow'.
I have tried finding a working example but I can't. Any help is greatly appreciated!
Try this
protected void ddlAddLabTest_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlLabTest = (DropDownList)sender;
DataGridItem row = (DataGridItem) ddlLabTest.NamingContainer;
DropDownList ddlAddLabTestShortName = (DropDownList)row.FindControl("ddlAddShortname");
ddlAddLabTestShortName.SelectedIndex = intSelectedIndex;
}
Seems like the NamingContainer is not a row, so leave it like that. It already has the FindControl method.
var row = ddlLabTest.NamingContainer;

Parameterised Drill down with Gridview

I'm trying to get a Gridview "select" link to drill down to a specific page for the selected row in ASP.NET with C#. This gridview is generated dynamically in the page_load, and is databound from a fairly simple SQL select query. Each row has a unique id, and I would like this to be passed as a parameter in the url when the select button is clicked for that row. So, when you click the "select" button on the row with an id value of 9 (note - not the id as defined by the gridview, but the one from the SQL query) you are redirected to an address such as moreDetail.aspx?id=9.
However, when trying to pass the id into the event handler I've hit issues... GridView.SelectedIndexChanging takes the usual (object sender, EventArgs e) as parameters and nothing else, and since the Gridview is created at Page_Load the EventArgs class is useless. I can't seem to find any way to pass the id that I have retrieved earlier into the event handler.
After lots of searching,I tried creating a class that extends EventArgs (obviously with my extra parameter added in), but it seems using any parameters other than (object sender, EventArgs e) just won't work. I could theoretically redo the SQL query within the event handler, but that seems to me a terrible way to achieve what I'm looking for, so I'm hoping someone will be able to see what I've got wrong here, because I'm sure I'm missing something obvious.
Some code - grid.SelectedRow.Cells[0] will contain the parameter I want to pass:
In Page_Load:
GridView grid = new GridView();
grid.DataSource = source;
CommandField selectField = new CommandField();
selectField.ShowSelectButton = true;
selectField.SelectText = "View Jobs";
grid.Columns.Add(selectField);
grid.SelectedIndexChanging += grid_SelectedIndexChanging;
grid.DataBind();
content.Controls.Add(grid);
And the Event Handler:
protected void grid_SelectedIndexChanging(object sender, EventArgs e)
{
Response.Redirect("ViewCustomer.aspx?id=" + grid.SelectedRow.Cells[0]);
}
Obviously this doesn't work because the scope of grid doesn't extend to the handler...but how can I get access to that data?
You need to cast sender to GridView to reference your calling GridView:
protected void grid_SelectedIndexChanging(object sender, EventArgs e)
{
GridView grid = (GridView)sender;
Response.Redirect("ViewCustomer.aspx?id=" + grid.SelectedRow.Cells[0]);
}

adding footer value to datagridview in C#

I am having 2 datatables. One is having the actual value of that table selected by the user and the next is having the aggregate value for that table i.e any grand total or avg. I want to display this as the footer in C# datagridview. How can I do that??? In asp.net we have RowDataBound event like that similar something is there in C# also but what it is i'm not able to find.
You can use ondatabound event of GridView for populating footer. Sample:
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridView grid = sender as GridView;
grid.FooterRow.Cells[0].Text = CustomDataSource.GetAggregated.Rows[0]["Name"].ToString();
grid.FooterRow.Cells[1].Text = CustomDataSource.GetAggregated.Rows[0]["Total"].ToString();
}

Categories

Resources