Datagrid plus drop-down list dropdownlist and dropdownlist
Code in the datagrid:
<Asp: datagrid id = "dgList" runat = "server" ItemStyle-HorizontalAlign = "Center" HeaderStyle-HorizontalAlign = "Center" HorizontalAlign = "Center" PageSize = "5" AutoGenerateColumns = "False" gridLines = "Horizontal" CellPadding = "1" BackColor = "White" BorderWidth = "1px" BorderStyle = "None" BorderColor = "# E7E7FF" Width = 100% OnItemDataBound = "Maid itemdatabound"> <FooterStyle ForeColor = "# 4A3C8C" BackColor = "# B5C7DE"> </FooterStyle> <SelectedItemStyle Font-Bold = "True" ForeColor = "# F7F7F7" BackColor = "# 738A9C"> </SelectedItemStyle> <AlternatingItemStyle BackColor = "# F7F7F7"> </AlternatingItemStyle> <ItemStyle placement = "Center" ForeColor = "# 4A3C8C" BackColor = "# E7E7FF"> </ itemStyle> <HeaderStyle Font-Bold = "True" HorizontalAlign = "Center" ForeColor = "# F7F7F7" BackColor = "# 4A3C8C"> </HeaderStyle> <Columns> <asp: templatecolumn HeaderText = "status"> <itemtemplate> <asp: dropdownlist id = "ItemDropDown" runat = "server" AutoPostBack = "true" OnSelectedIndexChanged = "DropDown_SelectedIndexChanged"/> </itemtemplate> </asp: TemplateColumn> <asp: templateColumn> <ItemTemplate> <asp: Button id = "SaveButton" Visible = "false" runat = "server" CommandName = "save" Text = "save"> </asp: button> </ItemTemplate> </asp: templatecolumn> <PagerStyle ForeColor = "# 4A3C8C" BackColor = "# E7E7FF" Mode = "NumericPages"> </PagerStyle> </asp: datagrid>
Private void InitializeComponent () {this. dgList. itemCommand + = new System. web. UI. webControls. dataGridCommandEventHandler (this. dgList_ItemCommand);} protected void maid (object sender, DataGridItemEventArgs e) {if (e. item. itemType = ListItemType. alternatingItem | e. item. itemType = ListItemType. item) {string [] options = {"", "notified", "Change email", "unable to contact", "cancel Merchant"}; DropDownList li St = (DropDownList) e. item. findControl ("ItemDropDown"); list. dataSource = options; list. dataBind (); TableCell cell = list. parent as TableCell; DataGridItem item = cell. parent as DataGridItem; int index = item. itemIndex; list. selectedValue = item. cells [1]. text ;}} protected void DropDown_SelectedIndexChanged (object sender, EventArgs e) {DropDownList list = (DropDownList) sender; TableCell cell = li St. parent as TableCell; DataGridItem item = cell. parent as DataGridItem; int index = item. itemIndex; string content = item. cells [12]. text; // DropDownList down = (DropDownList) item. findControl ("ItemDropDown"); string checkState = list. selectedValue. trim (); object obj = item. cells [13]. findControl ("queryButton"); if (obj! = Null) {Button lb = (Button) obj; lb. visible = true; // The Save button is displayed in the dynamic drop-down list} private void dgList_ItemCommand (object source, System. web. UI. webControls. dataGridCommandEventArgs e) {string spid = e. item. cells [2]. text. trim (); string crt_etime = e. item. cells [8]. text. trim (); string state = e. item. cells [12]. text. trim (); try {if (e. commandName = "save ");.............................. // Save} catch (Exception eSys) {WebUtils. ShowMessage (this. Page, "failed to save data! "+ ESys. Message );}}
3. How to add a column like this in the DataGrid: A DropDownList column, and the DropDownList of each row can be
Insert a template column in The DataGrid
<Asp: TemplateColumn HeaderText = "drop-down box column">
<ItemTemplate>
<Asp: dropdownlist id = "ddlCopyFileQty" runat = "server" Width = "52px">
<Asp: ListItem Value = "0"> 0 </asp: ListItem>
</Asp: dropdownlist>
</ItemTemplate>
<HeaderStyle Width = "60px"/>
</Asp: TemplateColumn>
Your question says "and every row's DropDownList can be used" is still incomplete. I guess whether to obtain the DropDownList of each row for value assignment or value operation:
Foreach (DataGridItem dt in dgInfoList. Items)
{
DropDownList ddlCopyFileQty = (DropDownList) dt. Cells [3]. FindControl ("ddlCopyFileQty ");
DdlCopyFileQty. SelectedValue = dt. Cells [6]. Text;
}
How can I dynamically insert data in the drop-down box set in the DATAGRID in aspnet?
Use Template
Add data to dd in itembound event