一、图解
下图为效果图,点击 【A表】种的某一行,【B表】会有与之相对于一行会被自动选中并且勾选上;
二、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_; } ////// 保存 /// 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(); } ////// 保存按钮 /// /// /// protected void Button1_Click(object sender, EventArgs e) { SaveUserRole(); } }