how to get dropdown value inside gridview on button click in c#? - c#

I have tried this but its not working,giving null value:please tell me what have to change in this code.
protected void Button1_Click(object sender, EventArgs e)
{
GridViewRow row;
row = dgData.Rows[0];
DropDownList ddl= (DropDownList)(row.Cells[1].FindControl("ddlCol1"));
}

use
DropDownList ddl= (DropDownList)(row.FindControl("ddlCol1"));
and try and let me know

try this code on click event
foreach (GridViewRow row in GridView1.Rows)
{
//Finding Dropdown control
DropDownList ddl1 = row.FindControl("ddlTest") as DropDownList;
if (ddl1 != null)
{
// your code here
}
}

#creby i saw your code.
you are adding your dropdown in grid view during row data bound event ok.. but now when you click on the button, all the drop down will be vanished so that's why you will not able to get drop down.
use item template of grid view and then bind dropdown in row data bound event.

Related

how to get the rows property in gridview of devxpress with a button to edit?

i have a gridview with devexpress my gridview contain a id ID="GriviewLV1" and i have a button in the row of the gridview to edit the record , in the event click of the button im trying give click in the button to edit but cannot get the property rows or something like that. im trying something like this but cannot do rows property because dont exist the rows property in grid view devexpress
button_Edit_click(object sender, EventArgs e)
{
foreach (GridViewRow row in GriviewLV1.Rows)
{
}
}
And for what exactly do you need row collection?
As far as I know, you can only manipulate with records (DataSource property object). Also it is possible to get selected row. Some my code examples with explanation:
private List<ImageSetMember> imageSets;
...
//assign collection as grid's DataSource.
//From now on any actions on imageSets object will be automatically reproduced
//as grid's row changes.
imageSetGridControl.DataSource = imageSets;
...
private void replaceButtonEdit_Click(object sender, System.EventArgs e)
{
//get focused row record's index at DataSource collection
int index = imageSetGridView.GetDataSourceRowIndex(imageSetGridView.FocusedRowHandle);
var selectedImage = imageSet[index].Image; //accessing to row's record
}

to populate one dropdown on selectedindexchanged of another inside gridview

I have gridview with two dropdowns in a row say country and state,on change of country dropdown i want to populate the state dropdown when the grid is in edit state.
I got both the dropdowns in RowEditing event of grid,also selectedindexchanged event is attached to first dropdown of grid.The problem is how to get second dropdown i.e. state dropdown in selectedindexchange event of country dropdown.
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl1 = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl1.NamingContainer;
if (row != null)
{
DropDownList ddl2 = (DropDownList)row.FindControl("DropDownList3");
{
//call the method for binding the second DDL based on the selected item on the first DDL
DataTable dt = BindDropDownList(ddl1.SelectedItem.Text);
ddl2.DataTextField = "Field1";
ddl2.DataValueField = "Field2";
ddl2.DataBind();
}
}
}
If RowEditing event occurs before dropdown selection changed event, you can store reference to current Row in you application. Then get this row in dropdown changed event.

getting command id from drop down list in a grid view

i inserted a drop down list into my grid view from the code behind like it is done here
Convert gridview field into Dropdownlist
i need to make command in the row that contains this dropdownlist when its selected index is changed.
please can some one hepl me
thanks
If you need to get the GridViewRow of a DropDownList which was generated dynamically use following approach.
Add the same SelectedIndexChanged-event handler to the dropdowns. Handle it, cast sender to DropDownList, cast it's NamingContainer to GridViewRow. Now you are all set.
protected void DropDown_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList) sender;
GridViewRow row = (GridViewRow) ddl.NamingContainer;
// now you get the reference to the other controls via row.FindControl
}

Dropdownlist values comes null on button click.Dropdownlist is placed at first row of gridview

Here is code that i used to access dropdownlist inside gridview on button click.I know its due to postback of page.But how to fix it.
protected void Button1_Click(object sender, EventArgs e)
{
GridViewRow row;
row = dgData.Rows[0];
DropDownList ddl= (DropDownList)(row.Cells[1].FindControl("ddlCol1"));
}
On page load i have called the method to bind gridview.
if (page.ispostback==false)
{
grdbind();
}
try with 0th cell if it is first
(row.Cells[0].FindControl("ddlCol1"))

How to obtain the value of HiddenField in a GridView when a CheckBox is checked?

I have a GridView which has multiple rows, on each row I have a CheckBox and a HiddenField. On button click I want to check if the CheckBox is checked and if it is I want to take the value of the HiddenField for that row. Each HiddenField on each row has a different value. User could check multiple CheckBoxes so I need to be able to pull the value of each HiddenField.
Any help will be really appreciate it.
Thank you
Loop through each row in the grid, check if the checkbox is checked and if it is, grab the value of the hidden field.
foreach (GridViewRow row in grdView.Rows)
{
if((row.FindControl("chkBoxId") as CheckBox).Checked)
{
string hiddenFieldValue = (row.FindControl("hiddenFieldId") as HiddenField).Value;
}
}
Where chkBoxId is the ID property of your checkbox on the page and hiddenFieldId is the ID of the hiddenfield control on your page.
Possible duplicates.
How to get values of CheckBoxes inside a gridview that are checked using asp .net
Get the id of selected checkboxes in gridview (Asp.net) c#
How to get the value in the gridview which the checkbox is checked?
One of the answer in above links :
foreach(Gridviewrow gvr in Gridview1.Rows)
{
if(((CheckBox)gvr.findcontrol("CheckBox1")).Checked == true)
{
//Get hidden field value here.
}
}
You can use a code like this:
protected void BtnMybutton_click( Object sender, EventArgs e)
{
Button Mybutton = (Button) sender;
GridViewRow row = (GridViewRow) MyButton.NamingContainer;
CheckBox ChkTest = (CheckBox) row.FindControl("ChkTest");
HidenFiekd HdfValue = (HidenField) row.FindControl("HdfValue");
if(ChkTest.Checked)
{
Console.WriteLine(HdfValues.Value);
}
}

Categories