how to change drop down list based on gridview selection - c#

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();
}

Related

DropDownList: SelectedIndexChanged do not fire

I fill a ASP Dropdownlist in the pageload
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
for (int i = 0; i < _maxStationsAnz; i++)
{
String stations = arrayList[1];
_allData = stations.Split(Convert.ToChar("/"));
_data = _allData[i].Split(Convert.ToChar(";"));
ddWeatherstations.Items.Add(_data[0]);
}
}
When a new station is selected, it should update informations. I use the OnSelectedIndexChanged attribute on my aspx site.
My code behind:
protected void ddWeatherstations_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < _maxStationsAnz; i++)
{
if (ddWeatherstations.SelectedIndex == i)
{
lselected.Text = "Index changed!";
//unimportant code
//....
}
}
}
When I set a breakpoint and run the program and change the value of the dropdownlist, nothing happens.
You need to set AutoPostBack property of your dropdownlist ddWeatherstations to True.
Try This:
<asp:DropDownList ID="ddWeatherstations" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddWeatherstations_SelectedIndexChanged">
</asp:DropDownList>
You need to set property AutoPostBack="true" but this will post back all of your page every time you select a different index of your DropDownList so I will suggest you to put your DropDownList in an AJAX Update Panel.

Select and show selected row to label

So, when I click Select I want to show in a Label all datas that contain one row. I have managed to make this, except DropdownList. When I click "Select" it's just empty.
protected void GridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
Label1.Text = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
Label2.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
Label3.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
}
P.S.: I have not done this programmatically. The only code I've wrote on .aspx.cs file is the code above.
Use this to find the value. its not showing the value because of template field control.
I have used the gridview control that you pasted in pastebin.
protected void GridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) {
Label drpValue =
(Label)this.GridView1.Rows[e.NewSelectedIndex].Cells[1].FindControl("Label1");
Lbl1.Text = drpValue.Text;
Lbl2.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
Lbl3.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
}

Show dropdown selected value in the grid on the same page

I have a drop down which has the list of delegates. When the user selects a delegate then I populate the available meet-timings in the second dropdown using the selectedindexchange event of the 1st dropdown
Aspx page
<asp:DropDownList ID="delegate_ddl" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddldelegates_SelectedIndexChanged" Width="200px"></asp:DropDownList>
<asp:DropDownList ID="delegatetime_ddl" runat="server" Width="90px"></asp:DropDownList>
<asp:Button ID="adddelegate" runat="server" Text="Add" onclick="adddelegate_Click"/>
.cs page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds1 = getdata.getdelegatelist();
delegate_ddl.DataSource = ds1.Tables[0];
delegate_ddl.DataTextField = "DELEGATE_NAME";
delegate_ddl.DataValueField = "DELEGATE_ID";
delegate_ddl.DataBind();
delegate_ddl.Items.Insert(0, "--Select--");
}
}
protected void ddldelegates_SelectedIndexChanged(object sender, EventArgs e)
{
string delselection = delegate_ddl.SelectedValue.ToString();
DataSet ds2 = getdata.getdelegatetimelist(delselection);
if (ds2.Tables[0].Rows.Count > 0)
{
delegatetime_ddl.DataSource = ds2.Tables[0];
delegatetime_ddl.DataTextField = "TIMESLOT";
delegatetime_ddl.DataValueField = "TIMEID";
delegatetime_ddl.DataBind();
}
else
{
time_lbl.Text = "No slots Open";
}
}
protected void adddelegate_Click(object sender, EventArgs e)
{
string delegateselected = delegate_ddl.SelectedValue.ToString();
string timeslotselected = delegatetime_ddl.SelectedValue.ToString();
getdata.delegatemeetinsert(personidd, delegateselected, timeslotselected);
}
Now the data gets inserted – but my question here is
As soon as the user click add button I would like to display the delegate selected and the time slot selected in a some sort of a grid view or dynamic table below with a delete option.
Can someone please provide a code sample in C# to achieve the above
Instead of using Gridview for displaying data use some basic control like Labels,it will reduce lot's of unncessary code. use Asp.net 'Panel' control and encapsulate all label and button(for delete purpose) into Panel then hide/show according to need. here is the outline of code that might help you,
if(!page.IsPostBack) // This goes into Page_Load
{
Panel1.Visible=false;
}
protected void adddelegate_Click(object sender, EventArgs e) // add this additional code
{
Panel.Visible=True;
GetDelegate()// This method retrieve the delegate you inserted..
Lable1.Text= "Set here Delegate name you just Retrieved"
Label2.Text="Delegate time you retrived"
}
protected void BtnRemovedelegate_Click(object sender, EventArgs e)
{
string Personidd= retrieve person id
string delegateName= Lable1.text;
String timeslot=Label2.Text
SomeDeleteMethod(personidd, delegateName, timeslot);
Panel1.Visible=false;
}
Hope you get the idea..

How to populate textbox with value of gridview control?

With just a regular value you would write something like
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow email = GridView1.SelectedRow;
txtbox.Text = email.Cells[5].Text;
}
However, I want to populate that textbox with a value that is a textbox control in the gridview. I have a list of rows and depending on which row I select that textbox will populate with that control value. Any help/advice would be greatly appreciated.
I would suggest using datakeys for this. It's a lot easier and a lot more reliable than using the cell index:
<asp:GridView ID="Gridview1" runat="server" DataKeyNames="Column1, Column2" ...>
Then, in the code-behind you can access the values like this:
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow email = GridView1.SelectedRow;
txtbox.Text = (string)GridView1.DataKeys[email.RowIndex]["Column1"];
}
Try this:
GridView1.RowCommand += GridView1_RowCommand;
private void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int index = Convert.ToInt32(Convert.ToString(e.CommandArgument));
GridViewRow row = GridView1.Rows[index];
this.NameTextBox.Text = Server.HtmlDecode(row.Cells[1].Text);
}
}
Try below code.
TextBox tb = GridView1.SelectedRow.FindControl("textboxId") as TextBox;
textbox.Text = tb.Text;
You can find a better code here on how to populate gridview with the text box values of your form.
http://sharepoint-2010-world.blogspot.in/2013/10/populating-grid-with-form-values.html

Accessing gridview from a non GridView event

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

Categories

Resources