Binding data to datagrid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jharikrishna
    New Member
    • Aug 2006
    • 15

    Binding data to datagrid

    Hi frnds

    How to bind data from dropdownlist and listbox to datagrid
  • bhar
    Banned
    New Member
    • Apr 2006
    • 37

    #2
    Hi,

    <asp:DataGrid
    id="DataGrid1" runat="server"
    AutoGenerateCol umns="False"
    OnItemDataBound ="DataGrid1_Ite mDataBound">
    <Columns>
    <asp:BoundColum n DataField="au_f name" HeaderText="au_ fname" />
    <asp:TemplateCo lumn>
    <HeaderTemplate >
    <asp:DropDownLi st
    ID="HeaderDropD own" Runat="server"
    AutoPostBack="T rue"
    OnSelectedIndex Changed="DropDo wn_SelectedInde xChanged" />
    </HeaderTemplate>
    <ItemTemplate >
    <asp:DropDownLi st
    ID="ItemDropDow n" Runat="server"
    AutoPostBack="T rue"
    OnSelectedIndex Changed="DropDo wn_SelectedInde xChanged" />
    </ItemTemplate>
    </asp:TemplateCol umn>
    </Columns>
    </asp:DataGrid>


    We have a DropDownList in the header declared as HeaderDropDown, and a DropDownList in the Item template declared as ItemDropDown. Notice we set the AutoPostBack property to true. Setting AutoPostBack to true allows the form to post back to the server and raise an event each time the user changes a selection in the DropDownList control. We also assign an event handler for the SelectedIndexCh anged event. Notice we are sharing the same event handler (DropDown_Selec tedIndexChanged ) for all DropDownList controls.

    protected void DataGrid1_ItemD ataBound(object sender, DataGridItemEve ntArgs e)
    {
    if(e.Item.ItemT ype == ListItemType.Al ternatingItem ||
    e.Item.ItemType == ListItemType.It em)
    {
    string[] options = { "Option1", "Option2", "Option3" };

    DropDownList list = (DropDownList)e .Item.FindContr ol("ItemDropDow n");
    list.DataSource = options;
    list.DataBind() ;
    }
    else if(e.Item.ItemT ype == ListItemType.He ader)
    {
    string[] options = { "OptionA", "OptionB", "OptionC" };

    DropDownList list = (DropDownList)e .Item.FindContr ol("HeaderDropD own");
    list.DataSource = options;
    list.DataBind() ;
    }
    }


    protected void DropDown_Select edIndexChanged( object sender, EventArgs e)
    {
    DropDownList list = (DropDownList)s ender;

    TableCell cell = list.Parent as TableCell;
    DataGridItem item = cell.Parent as DataGridItem;

    int index = item.ItemIndex;
    string content = item.Cells[0].Text;

    Response.Write(
    String.Format(" Row {0} contains {1}", index, content)
    );

    }

    Try this out.

    Regards
    bhar
    [Link Removed]
    Last edited by Frinavale; Apr 27 '07, 02:50 PM. Reason: Link was removed because it is considered spam and its content is not typical HTML

    Comment

    Working...