When the gridview contains a dorpdownlist, use the built-in template editing method.
Today, we will record the method that uses the built-in editing mode in the gridview if dropdownlist exists.
Well, today's question is a bit difficult. Let's explain it in detail.
Because the data of some columns in the gridview is carried out from basedata, when editing the gridview, the user wants to manually select the column value, rather than manually input (if the input is incorrect, the system reports an error). The above is the background.
OK, I thought about it. In the gridview, this function can be implemented in this way. Use the template that comes with the gridview to edit the data, use label to bind the data presentation, and use dropdownlist when editing the data. The following code is pasted:
Front-end:
<Asp: gridView ID = "GridView1" runat = "server" AutoGenerateColumns = "False" OnRowCancelingEdit = "inline" OnRowDeleting = "inline" OnRowEditing = "inline" OnRowUpdating = "inline" OnRowDataBound = "inline ""> <Columns> <asp: templateField HeaderText = "category"> <ItemTemplate> <asp: Label ID = "label1" runat = "server" Text = '<% # Bind ("category ") %> '> </asp: Label> </ItemTemplate> <EditItemTemplate> <asp: label ID = "label2" runat = "server" Text = '<% # Bind ("category") %> 'cssclass = "hideColumn"> </asp: label> <asp: DropDownList ID = "drpCategory" runat = "server" Width = "156px"> </asp: DropDownList> </EditItemTemplate> </asp: templateField> <asp: CommandField HeaderText = "edit" ShowEditButton = "True"/> <asp: commandField HeaderText = "delete" ShowDeleteButton = "True"/> </Columns> </asp: GridView>View Code
Background:
Protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {BindGridView () ;}} protected void GridView1_RowEditing (object sender, GridViewEditEventArgs e) {GridView1.EditIndex = e. newEditIndex; BindGridView ();} protected void GridView1_RowDeleting (object sender, subject e) {} protected void GridView1_RowUpdating (object sender, subject e) {} protected void evaluate (object sender, gridViewCancelEditEventArgs e) {GridView1.EditIndex =-1; BindGridView ();} protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e) {if (e. row. rowType = DataControlRowType. dataRow) {int itest = e. row. rowIndex; if (GridView1.EditIndex = 0) {Label label2 = (Label) e. row. findControl ("label2"); string strCategory = label2.Text. trim (); DropDownList drpCategory = (DropDownList) e. row. findControl ("drpCategory"); BindDropDownList (drpCategory, strCategory) ;}} private void Merge (DropDownList drpCategory, string strCategory) {DataTable dt = new able (); dt. columns. add ("category"); drpCategory. dataSource = dt; drpCategory. dataBind (); drpCategory. dataTextField = "category"; drpCategory. dataValueField = "category"; drpCategory. items. insert (0, new ListItem ("select", "0"); drpCategory. items. insert (1, new ListItem ("test1", "test1"); drpCategory. items. insert (2, new ListItem ("test2", "test2"); drpCategory. items. insert (3, new ListItem ("test", "test"); drpCategory. selectedValue = strCategory;} private void BindGridView () {DataTable dt = new DataTable (); dt. columns. add ("category"); // dt. rows. add (dt. newRow (); DataRow row = dt. newRow (); row ["category"] = "test"; dt. rows. add (row); this. gridView1.DataSource = dt; this. gridView1.DataBind ();}View Code
Effect:
Click to edit
Display dropdownlist and bind the label Value
OK. Now, go to bed ~ Continue to work tomorrow Monday!