I am using two dropdownlist in gridview. Total 5 to 7 rows in gridview.
Bind ProductId and product name in First Dropdown. when select product that time databound in second dropdown.
so how to findout row number when fire dropdown's event.
I assume the property you're looking for is GridViewRow.RowIndex. To get the reference to the row from the SelectedIndexChanged event from your DropDownLis(s), you can use it's NamingContainer property:
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
int rowIndex = row.RowIndex;
// if you want to get the reference to the other DropDown in this row
DropDownList ddl2 = (DropDownList)row.FindControl("DropDownList2");
}
You can handle SelectedIndexChanged event of DropDownList.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)((DropDownList)sender).Parent.Parent;
string rowNumber= GridView1.DataKeys[gvr.RowIndex].Value.ToString();
}
Related
get the Product SKU Of the row which the button pressed
protected void On_Update_Click(object sender, EventArgs e)
{
}
The easiest way is to use the DataKeyNames property.
<asp:GridView ID="GridView1" runat="server" DataKeyNames="SKU">
And then you can access the correct DataKeyNames based on the Row Index.
protected void On_Update_Click(object sender, EventArgs e)
{
//cast the sender back to a control and get the gridviewrow from the namingconainer
GridViewRow row = (GridViewRow)(((Control)sender).NamingContainer);
//get the gridview itself from the gridviewrow
GridView gv = row.Parent.Parent as GridView;
//get the correct datakey from the gridview with the dataitemindex
int SKU = Convert.ToInt32(gv.DataKeys[row.DataItemIndex].Values[0]);
//display result
Label1.Text = SKU.ToString();
}
I try to delete a record in Gridview and Database .I added a boundfield button and set the CommandName to Deleterecord in the RowCommand event I want to get the primary key of this record to delete that(primary key value does not show in the grid view. The following block of code shows this event(I try to show some data in a text box):
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Deleterecord")
{
TextBox1.Text = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
//bla bla
}
}
I also set
DataKeyName="sourceName"
in the gridview but it is not my primary key
If I click this button an exception occured like this:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
How can I solve this problem
Use this
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Deleterecord")
{
LinkButton lb = (LinkButton)e.CommandSource;
GridViewRow gvr = (GridViewRow)lb.NamingContainer;
TextBox1.Text = grid.DataKeys[gvr.RowIndex].Value.ToString();
//bla bla
}
}
Here LinkButton should be replaced by the type of control which is performing this RowCommand Event
For more info go Here
i need to get the value of the selected row of radiobutton in the gridview. the selected value is assigned to a textbox. my code is
protected void lnBTNDone_Click(object sender, EventArgs e)
{
GridViewRow gvRow = grdEventDetails.SelectedRow;
txtEventId.Text = gvRow.Cells[0].Text;
}
the problem is the gvRow value is assigned as null .
the linkbutton is not inside gridview. the gridview n linkbutton and the textbox are inside the user controls.
You can always get the GridViewRow via the sender's NamingContainer:
protected void lnBTNDone_Click(object sender, EventArgs e)
{
LinkButton lnBTNDone = (LinkButton)sender;
GridViewRow row = (GridViewRow)lnBTNDone.NamingContainer;
txtEventId.Text = row.Cells[0].Text;
}
Assuming that the LinkButton is in the selected row.
Btw, i'm yet not sure why SelectedRow is null there, maybe because the GridView's SelectedIndexChanged event is triggered after the LinkButton's click event.
protected void lnBTNDone_Click(object sender, EventArgs e)
{
for (int i = 0; i < grdEventDetails.Rows.Count; i++)
{
RadioButton rb = (grdEventDetails.Rows[i].FindControl("grdRdo")) as RadioButton;
if (rb.Checked == true)
{
txtEventId.Text = grdEventDetails.Rows[i].Cells[1].Text;
}
}
}
I'm working on asp.net website with Vb.net and I have a dropdownlist with autopostback = true and I need to get the selected value when I change the item or I want to get the item which fires the selectedindexchanged event ..
any help please..
In ie. your Page_Load set
this.ComboBox1.SelectedIndexChanged += new System.EventHandler(ComboBox1_SelectedIndexChanged);
Then write the event handler like this:
private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
string selected = (string) comboBox.SelectedItem;
}
Make sure that in your Page_Load you write this before setting the combobox default value or you will end up with this always being the selected item:
if (Page.IsPostBack)
return;
try this:
protected void list_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList list = (DropDownList)sender;
string value = (string)list.SelectedValue;
}
If item is a Dictionary:
string value = ((KeyValuePair<string, string>)combobox.SelectedItem).Key;
I have a dropdownlist in my Gridview and I am binding a datasource to the gridview.
Though all the records are displaying properly the dropdown value is not selected.
How do I set something like
<%# Bind("Country") %> for a dropdownlist in the Gridview in ASP.net.
Thanks
You can hook into the RowDataBound event for the grid view, find the control and set the value.
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
var dropdownList = e.Row.FindControl("YOUR_DROP_DOWN") as DropDownList;
dropdownList .SelectedIndex = SET_VALUE_HERE;
}
Setting DropDownList value from a Datasource should be like:
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlCountry = (DropDownList)e.Row.FindControl("ddlCountry");
ddlCountry.SelectedValue = DataBinder.Eval(e.Row.DataItem, "Country").ToString();
}
}