Devexpress: bind a drop-down box in aspxgridview (use a simple gridviewdatacomboboxcolumn instead of a damn aspxdropdownedit)

Source: Internet
Author: User

This damn aspxdropdownedit cannot solve the problem well, and it took a whole day to solve it. Just now, I visited the devexpree website and found the legendary solution: I did not recommend using aspxdropdownedit, but recommended ComboBox.

The solution is as follows:

1. html front-end code

Note: The key lies in sqldatasource, fieldname, textfield, valuefield, keyfieldname

<Asp: sqldatasource id = "sqldatasrcapp" runat = "server"> </ASP: sqldatasource> <asp: sqldatasource id = "sqldatasrcgroup" runat = "server"> </ASP: sqldatasource> <DX: aspxgridview id = "grvusergroup" runat = "server" autogeneratecolumns = "false" clientidmode = "autoid" cssfilepath = "~ /Configure/office2003blue/{0}/styles.css "csspostfix =" office2003blue "width =" 100% "keyfieldname =" appid "onrowinserting =" inline "onrowupdating =" inline "onrowdeleting =" begin "oncelleditorinitialize =" grvusergroup_celleditorinitialize "> <clientsideevents rowdblclick =" function (s, e) {S. starteditrow (E. visibleindex);} "/> <columns> <DX: gridviewdata Comboboxcolumn caption = "system name" fieldname = "appid" visibleindex = "0"> <propertiescombobox valuetype = "system. string "textfield =" appname "valuefield =" appid "enablesynchronization =" false "placement =" startswith "performanceid =" sqldatasrcapp "width =" 100% "> </propertiescombobox> <editcellstyle horizontalal = "Left"> </editcellstyle> </dx: gridviewdatacomboboxcolumn> <DX: gridviewdatacomboboxco Lumn caption = "User Group" fieldname = "groupid" visibleindex = "1"> <propertiescombobox valuetype = "system. string "textfield =" groupname "valuefield =" groupid "placement =" false "placement =" startswith "performanceid =" sqldatasrcgroup "width =" 100% "> </propertiescombobox> <editcellstyle horizontalalign = "Left"> </editcellstyle> </dx: gridviewdatacomboboxcolumn> </columns> <settingsbehavior Authorization = "true"/> <settingsediting mode = "popupeditform" popupeditformheight = "80px" placement = "windowcenter" popupeditformmodal = "true" placement = "windowcenter" placement = "450px" editformcolumncount = "4"/> <settingstext commandcancel = "cancel" commanddelete = "delete" commandedit = "modify" commandnew = "new" commandupdate = "OK" confirmdelete = "are you sure you want to delete? "Title =" modifying user group information" Popupeditformcaption = "Modify user group information"/> <styles cssfilepath = "~ /App_themes/office2003blue/{0}/styles.css "csspostfix =" office2003blue "> 

2. Main background code (1)

// If (! This. ispostback) {----} // The ellipsis contains the following content: ssql = "select [appid], [appname] from [sysapp]"; sqldatasrcapp. selectcommand = ssql; sqldatasrcapp. connectionstring = Common. confighelper. getconfigstring ("connectionstring"); // = Common. tool. getdatatable (Out serror, ssql); If (! String. isnullorempty (serror) {common. messageBox. show (this, serror); return;} ssql = "select [groupid], [groupname] from [sysgroup]"; sqldatasrcgroup. selectcommand = ssql; sqldatasrcgroup. connectionstring = Common. confighelper. getconfigstring ("connectionstring"); If (! String. isnullorempty (serror) {common. MessageBox. Show (this, serror); return ;}

3. Main background code (2)

        protected void grvUserGroup_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)        {            string sSQL = string.Empty;            if (grvUserGroup.IsNewRowEditing && e.Column.FieldName == "AppID")            {                sSQL = "select [AppID],[AppName] from [sysApp]";                sqlDataSrcApp.SelectCommand = sSQL;                sqlDataSrcApp.ConnectionString = Common.ConfigHelper.GetConfigString("ConnectionString");                //= Common.Tool.GetDataTable(out sError, sSQL);                if (!string.IsNullOrEmpty(sError)) { Common.MessageBox.Show(this, sError); return; }            }            else if (grvUserGroup.IsNewRowEditing && e.Column.FieldName == "GroupID")            {                sSQL = "select [GroupID],[GroupName] from [sysGroup]";                sqlDataSrcGroup.SelectCommand = sSQL;                sqlDataSrcGroup.ConnectionString = Common.ConfigHelper.GetConfigString("ConnectionString");                if (!string.IsNullOrEmpty(sError)) { Common.MessageBox.Show(this, sError); return; }            }        }

4. grvusergroup_rowinserting, grvusergroup_rowupdating, and grvusergroup_rowdeleting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.