Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
good morning

i have a dropdown in gridview.i am binding it to the database in which i am having some values.

when i run the program, it is showing the default value as --Select--

but i want to see the default item which is already selected.

please help me.

thank you.
Posted
Comments
Dharmendra-18 1-Mar-13 0:44am    
are you using datatable to attach data in dropdwonlist or directly ?
S. M. Ahasan Habib 1-Mar-13 0:48am    
Plese show your code though we can understand your problem better.

it's my previous problem
try to look here

assign dropdownlist text value[^]
 
Share this answer
 
Hi,

Use the below code,

XML
<asp:DataGrid
    id="DataGrid1" runat="server"
    AutoGenerateColumns="False"
    OnItemDataBound="DataGrid1_ItemDataBound">
    <Columns>
        <asp:BoundColumn DataField="au_fname" HeaderText="au_fname" />
        <asp:TemplateColumn>
            <HeaderTemplate>
                <asp:DropDownList
                    ID="HeaderDropDown" Runat="server"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="DropDown_SelectedIndexChanged" />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:DropDownList
                    ID="ItemDropDown" Runat="server"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="DropDown_SelectedIndexChanged" />
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:DataGrid>



protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
   if(e.Item.ItemType == ListItemType.AlternatingItem ||
      e.Item.ItemType == ListItemType.Item)
   {
      string[] options = { "Option1", "Option2", "Option3" };

      DropDownList list = (DropDownList)e.Item.FindControl("ItemDropDown");
      list.DataSource = options;
      list.DataBind();
   }
   else if(e.Item.ItemType == ListItemType.Header)
   {
      string[] options = { "OptionA", "OptionB", "OptionC" };

      DropDownList list = (DropDownList)e.Item.FindControl("HeaderDropDown");
      list.DataSource = options;
      list.DataBind();
   }
}


Refer the below link

http://odetocode.com/articles/231.aspx[^]

Regards,
Babu.K
 
Share this answer
 
v2
in page load use

C#
if(!isPostBack)
{
//write code or call function for bound your gridview here
}



Thanks
 
Share this answer
 
Comments
[no name] 1-Mar-13 1:31am    
I think it would be not work as DropDownlist is there in Gridview.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900