Find DropDownList In Datagrid consisting of a column of DropDownLists [Resolved]

Posted by Anu_dgr8 under Regular Expressions on 7/5/2010 | Views : 7143 | Status : [Member] | Replies : 7
I have a datagrid consisting of a column of dropdownlists..and i have a submit button below datagrid..after selecting something in dropdownlists(i can select some text in every row),i want to store these values in a table..so the prob here is how do i find the dropdownlists in each row?

Thanks and Regards,
Hoosha Boosha



Responses

Posted by: Vuyiswamb on: 7/5/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down

Resolved
I will share the code i have in one of my apps , First i have a GridView defined like this , look at the Bold pat that applies to you

     <asp:GridView ID="grdViewResources" runat="server" AllowSorting="True" AutoGenerateColumns="False"

BackColor="Moccasin" BorderColor="DarkOrange" BorderStyle="Solid" BorderWidth="2px"
CellPadding="0" DataKeyNames="ID" DataSourceID="SqlDataSourceResources" Font-Names="Verdana"
Font-Size="9pt" ForeColor="Black" PageSize="2" Style="border-right: darkorange 2px solid;
border-top: darkorange 2px solid; border-left: darkorange 2px solid; border-bottom: darkorange 2px solid"
Width="100%" Height="24px" OnRowDataBound="grdViewResources_RowDataBound1">
<RowStyle Font-Names="Verdana" Font-Size="9pt" />
<HeaderStyle BackColor="DarkOrange" Font-Names="Verdana" Font-Size="10pt" />
<AlternatingRowStyle BackColor="PapayaWhip" BorderWidth="0px" />
<Columns>
<asp:TemplateField HeaderText="Select" SortExpression="ID">
<ItemTemplate>
<asp:CheckBox ID="chkbx" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="Descr">
<ItemTemplate>
<asp:TextBox ID="txtBoxDescr" runat="server" Text='<%# Bind("Descr") %>' BackColor="White"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Available for Booking" SortExpression="AVAILABLE">
<ItemTemplate>
&nbsp;<asp:DropDownList ID="drplstAvailable" runat="server" SelectedValue='<%# Bind("AVAILABLE") %>' BackColor="Transparent">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="0">No</asp:ListItem>
<asp:ListItem Value="1" Selected="True">Yes</asp:ListItem>
</asp:DropDownList>

</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


Now as you can see you have a dropdownlist there and it has yes or no , now if i want to get the selected value for each record and save it in the database i will have to Loop thorough the gridview like this

foreach (GridViewRow row in GridView2.Rows)
{

DropDownList drplstAvailable = (DropDownList)row.FindControl("drplstAvailable");

String YourValue = drplstAvailable.SelectedValue;

}


this is just a skeleton of the code , just fix it and it will be fine.

Thank you for posting at Dotnetfunda
[Administrator]

Anu_dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 7/5/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
if the Dropdown is in a templatefield then you can use the FindControl() method to find the control in the grid

Thank you for posting at Dotnetfunda
[Administrator]

Anu_dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anu_dgr8 on: 7/5/2010 [Member] Starter

Up
0
Down
yeah i hv used

DropDownList ddl = (DropDownList)datagrid1.FindControl("ddlinput");

but ddl's value gets set tO NULL WHEN I PUT A BREAKPOINT AT THIS LINE AND RUN

Thanks and Regards,
Hoosha Boosha

Anu_dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anu_dgr8 on: 7/5/2010 [Member] Starter

Up
0
Down
I HV PUT DROPDOWNIST INSIDE TemplateColumn ONLY

Thanks and Regards,
Hoosha Boosha

Anu_dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Syedshakeer on: 7/5/2010 [Member] Starter

Up
0
Down
Hi,
DropDownList ddl=(DropDownList)e.Item.Cells[1].FindControl("DropDownList1");

string selectedid=ddl.SelectedItem.Value;


Syed Shakeer Hussain

Anu_dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anu_dgr8 on: 7/7/2010 [Member] Starter

Up
0
Down
Hi Vuyiswamb!

Thanks alot!

Thanks and Regards,
Hoosha Boosha

Anu_dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 7/7/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Thank you for posting at Dotnetfunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Anu_dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response