Friday, October 3, 2008

How to bind the data from dropdownlist of gridview control and how to retrieve the data from grid view .

<ItemTemplate>
<asp:DropDownList ID="DropDownList1" DataTextField="Name"
DataValueField = "Name" DataSource= '<%# PopulateControls() %>' runat="server">
</asp:DropDownList>
</ItemTemplate>


public DataSet PopulateControls()
{
SqlConnection myConnection = new SqlConnection(GetConnectionString());
SqlDataAdapter ad = new SqlDataAdapter("SELECT [Name] FROM tblPerson",
myConnection);
DataSet ds = new DataSet();
ad.Fill(ds, "tblPerson");
return ds;


}

foreach (GridViewRow row in GridView1.Rows)
{
// Selects the text from the TextBox

// which is inside the GridView control

string textBoxText = _
((TextBox)row.FindControl("TextBox1")).Text;
Response.Write(textBoxText);
// Selects the text from the DropDownList

// which is inside the GridView control

string dropDownListText = ((DropDownList)
row.FindControl("DropDownList1")).SelectedItem.Value;
Response.Write(dropDownListText);
// Selects items from the ListBox

// which is inside the GridView control

ListBox myListBox = (ListBox)row.FindControl("ListBox1");

foreach(ListItem selectedItem in myListBox.Items)
{
// Checks if the item in the ListBox is selected or not

if (selectedItem.Selected)
{
// Print the value of the item if its selected

Response.Write(selectedItem.Value);
}
}



}

No comments: