ASPxGridView 选中主表一行数据,从表自动选中(勾选)对应的行
一、图解
下图为效果图,点击 【A表】种的某一行,【B表】会有与之相对于一行会被自动选中并且勾选上;
二、Html 代码
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script type="text/JavaScript"> function gridRole_SelectionChanged(s, e) { s.GetSelectedFieldValues("TyID", GetSelectedFieldValuesCallback); } function GetSelectedFieldValuesCallback(values) { selList.BeginUpdate(); try { selList.ClearItems(); for (var i = 0; i < values.length; i++) { selList.AddItem(values[i]); } } finally { selList.EndUpdate(); } } </script> </head> <body> <form id="form1" runat="server"> <div> <table style="width: 100%;"> <tr> <td> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> </td> <td> </td> </tr> <tr> <td> <dx:ASPxGridView ID="grid" ClientInstanceName="grid" runat="server" Width="300px" AutoGenerateColumns="false" KeyFieldName="YgID" ClientIDMode="AutoID" OnCustomColumnDisplayText="grid_CustomColumnDisplayText"> <Styles Row-Cursor="pointer"> <Row Cursor="pointer"> </Row> <AlternatingRow Enabled="True"> </AlternatingRow> </Styles> <ClientSideEvents FocusedRowChanged="function(s, e){ gridRole.PerformCallback(s.GetFocusedRowIndex()); }" /> <Columns> <dx:GridViewDataTextColumn Caption="序号" VisibleIndex="1" Width="5%"> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn Caption="账号" FieldName="YgID" ShowInCustomizationForm="True" VisibleIndex="2"> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn Caption="姓名" FieldName="Name" VisibleIndex="3"> </dx:GridViewDataTextColumn> </Columns> <SettingsBehavior AllowFocusedRow="True" /> <SettingsPager AlwaysShowPager="True"></SettingsPager> <Styles> <AlternatingRow Enabled="True"></AlternatingRow> <Row Cursor="pointer"></Row> </Styles> </dx:ASPxGridView> </td> <td> <dx:ASPxGridView ID="gridRole" ClientInstanceName="gridRole" runat="server" Width="200px" AutoGenerateColumns="False" KeyFieldName="TyID" ClientIDMode="AutoID" OnCustomCallback="gridRole_CustomCallback"> <SettingsPager AlwaysShowPager="True"> </SettingsPager> <Styles> <Row Cursor="pointer"> </Row> <AlternatingRow Enabled="True"> </AlternatingRow> </Styles> <Columns> <dx:GridViewCommandColumn ShowSelectCheckbox="True" VisibleIndex="0" Width="5%"> <ClearFilterButton Visible="True"> </ClearFilterButton> <HeaderStyle HorizontalAlign="Center" /> <CellStyle HorizontalAlign="Center"> </CellStyle> <HeaderTemplate> <input type="checkbox" onclick="gridRole.SelectAllRowsOnPage(this.checked);" title="全选" /> </HeaderTemplate> </dx:GridViewCommandColumn> <dx:GridViewDataTextColumn Caption="类型" FieldName="TyID" VisibleIndex="1"> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn Caption="角色名称" FieldName="TyName" VisibleIndex="2"> </dx:GridViewDataTextColumn> </Columns> <SettingsBehavior AllowFocusedRow="True" AllowMultiSelection="true" /> <Styles Row-Cursor="pointer"> <Row Cursor="pointer"> </Row> <AlternatingRow Enabled="True"> </AlternatingRow> </Styles> <ClientSideEvents SelectionChanged="gridRole_SelectionChanged" /> </dx:ASPxGridView> </td> </tr> <tr> <td colspan="2"> <dx:ASPxListBox ID="lsbRoleId" ClientInstanceName="selList" runat="server"> <Columns> <dx:ListBoxColumn Caption="编号" FieldName="TyID" /> </Columns> </dx:ASPxListBox> </td> </tr> </table> <div style="display: none;"> </div> </div> </form> </body> </html>
三、CS 代码
public partial class WebForm1 : System.Web.UI.Page { public static DataTable dtMaster;//用户主表 public static DataTable dtItem;//用户表 与 类型表的关联关系表 public static DataTable dtType;//类型表 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //..................................................................... //创建表 添加行列 //..................................................................... dtMaster = new DataTable(); dtMaster.Columns.Add("YgID",Type.GetType("System.String")); dtMaster.Columns.Add("Name", Type.GetType("System.String")); //---------------------- DataRow dr = dtMaster.NewRow(); dr["YgID"] = "Y01"; dr["Name"] = "同学1"; dtMaster.Rows.Add(dr); //.................. DataRow dr2 = dtMaster.NewRow(); dr2["YgID"] = "Y02"; dr2["Name"] = "同学2"; dtMaster.Rows.Add(dr2); //..................................................................... dtType = new DataTable(); dtType.Columns.Add("TyID", Type.GetType("System.String")); dtType.Columns.Add("TyName", Type.GetType("System.String")); //---------------------- DataRow Tydr = dtType.NewRow(); Tydr["TyID"] = "T01"; Tydr["TyName"] = "类型1"; dtType.Rows.Add(Tydr); //.................. DataRow Tydr2 = dtType.NewRow(); Tydr2["TyID"] = "T02"; Tydr2["TyName"] = "类型2"; dtType.Rows.Add(Tydr2); //..................................................................... dtItem = new DataTable(); dtItem.Columns.Add("YgID", Type.GetType("System.String")); dtItem.Columns.Add("TyID", Type.GetType("System.String")); //---------------------- DataRow dr1_ = dtItem.NewRow(); dr1_["YgID"] = "Y01"; dr1_["TyID"] = "T02"; dtItem.Rows.Add(dr1_); //.................. DataRow dr2_ = dtItem.NewRow(); dr2_["YgID"] = "Y02"; dr2_["TyID"] = "T01"; dtItem.Rows.Add(dr2_); //.................. this.grid.DataSource = dtMaster; this.grid.DataBind(); this.gridRole.DataSource = dtType; this.gridRole.DataBind(); } } protected void grid_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs e) { if (e.Column.VisibleIndex == 0) { e.DisplayText = (e.VisibleRowIndex + 1).ToString(); } } protected void gridRole_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e) { string userid = Convert.ToString(grid.GetRowValues(grid.FocusedRowIndex, "YgID")); ASPxGridView gd = sender as ASPxGridView; gd.Selection.UnselectAll(); DataTable dt = GetUsr(userid); for (int i = 0; i < gd.VisibleRowCount; i++) { string RoleIdRow = string.Empty; RoleIdRow = Convert.ToString((gd.GetRowValues(i, "TyID"))); if (RoleIdRow != string.Empty) { if (dt != null && dt.Rows.Count > 0) { for (int j = 0; j < dt.Rows.Count; j++) { string roleDs = Convert.ToString(dt.Rows[j]["TyID"]); if (RoleIdRow == roleDs) { gd.Selection.SelectRow(i); } } } } } } DataTable GetUsr(string usr) { DataTable dtItem_ = new DataTable(); dtItem_.Columns.Add("YgID", Type.GetType("System.String")); dtItem_.Columns.Add("TyID", Type.GetType("System.String")); for (int i = 0; i < dtItem.Rows.Count;i++ ) { if (usr == dtItem.Rows[i]["YgID"].ToString()) { DataRow dr = dtItem_.NewRow(); dr["YgID"] = dtItem.Rows[i]["YgID"].ToString(); dr["TyID"] = dtItem.Rows[i]["TyID"].ToString(); dtItem_.Rows.Add(dr); } } return dtItem_; } /// <summary> /// 保存 /// </summary> private void SaveUserRole() { string sUserId = Convert.ToString(grid.GetRowValues(grid.FocusedRowIndex, "UserId")); int iResult = 0; string RoldIdList = string.Empty; string url = string.Empty; for (int i = 0; i < lsbRoleId.Items.Count; i++) { RoldIdList += lsbRoleId.Items[i].ToString() + ','; } //## 插入到数据库语句 lsbRoleId.Items.Clear(); } /// <summary> /// 保存按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { SaveUserRole(); } }