Accessing gridview from a non GridView event - c#

I have a button outside a gridview called "UpdateAll", i need to be able to click on such button and find an item template "DropDownList" and update the database with that value for each record, I am not sure how to go about this any ideas?
public void UpdateAll_Click(object sender, EventArgs e)
{
}
Now I know I can access the drop down in the GridView_RowCommand something like this
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
Myddl = row.FindControl("Quantity") as DropDownList;
But i am not sure how to do that from an outside event that is not GridView related, I can not do the one above because it accesses the e.CommandSource.
Please help.

You can do something like this;
for(int rowIndex =0; rowIndex<gv.rows.count; rowIndex++)
{
Myddl = gv.rows[rowIndex].FindControl("Quantity") as DropDownList;
}

Related

How to keep values in textbox inside a binded GridView on ASP.NET?

I have a gridView, and inside is there exist a modifiable textbox.
I also have a dropdownlist, where if I click the dropdownlist, the system will read the database in SQL and display it in gridView.
How do I keep the number I previously typed in the first row of the gridView, when a new data is added after the dropdownlist is clicked?
Normally, the previously entered value of the textbox will be resetted if i rebind the gridView witthout refilling the textbox with a saved list or behaviour
Thank you very much in advance
I'm assuming you are using something like the OnSelectedIndexChanged event of the DropDownList. In there you can use FindControl to find the TextBox, store the data, rebind the GridView and set the value back to the TextBox. You need to use FindControl twice, one for the old and once for the new TextBox.
Note the use of oldValues.Count instead of GridView1.Rows.Count. If there were less rows in the new dataset than in the old you would get Index out of Bounds error.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
List<string> oldValues = new List<string>();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
TextBox tbOld = GridView1.Rows[i].FindControl("TextBox1") as TextBox;
oldValues.Add(tbOld.Text);
}
//rebind gridview here
for (int i = 0; i < oldValues.Count; i++)
{
TextBox tbNew = GridView1.Rows[i].FindControl("TextBox1") as TextBox;
tbNew.Text = oldValues[i];
}
}
You have used dropdownlist and textbox inside gridview?
if you click dropdownlist select value then postback to server and textbox value reset?

how to change drop down list based on gridview selection

I have a drop down list which will load up with a series of customer ID numbers. I then have a gridview control on my page with the select hyperlink. When i click on the link to select the row in gridview i would like my dropdown to change to that number.
Below is what i have tried but doesn't work:
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (!GridView1.SelectedIndex.Equals(-1))
{
DropDownList ddl;
ddl = (DropDownList)form1.FindControl("ddl_Customers");
ddl.SelectedValue = (String)GridView1.SelectedDataKey.Values[0];
}
}
Handle the SelectedIndexChanged event for GridView1
void GridView1_SelectedIndexChanged(Object sender, EventArgs e) {
ddl_Customers.SelectedValue = GridView1.SelectedDataKey.Value.ToString();
}
You can directly use GridView1.SelectedValue.ToString() for this. To use that, you should define a datakeyname like this: <asp:Gridview DataKeyNames="CustomerID">
Then all you need is this:
void GridView1_SelectedIndexChanged(Object sender, EventArgs e) {
ddl_Customers.SelectedValue = GridView1.SelectedValue.ToString();
}

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

How to transfer data from gridview to textbox

Suppose if I say I have selected a row in gridview and want that row to come up in to their respective textboxes. For example, I have selected a row which has First Name and Last Names and on selection of row, the data from gridview should come in to textbox on winform. Please tell me.
I am guess it is something based on selection changed event if I am right ? Like below:
private void dgv_SelectionChanged(object sender, EventArgs e)
{
string str = txtFirstName.Text;
DataGridViewRow selectedRow;
}
If I got your question right, your solution is to use DataGridView.SelectedRows Property along with DataGridView.SelectionChanged Event
DataGridView.SelectedRows Gets the collection of rows selected by the
user.
DataGridView.SelectionChanged occurs whenever cells are selected or
the selection is canceled, whether programmatically or by user action.
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows) {
//Send the first cell value into textbox'
Txt_FirstName.Text = row.Cells(0).Value.ToString; // or row.Cells["ColumnName"].Value;
}
}
}
MSDN and MSDN

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;

Categories

Resources