CheckedListBox与CheckedListBox联动

时间:2021-11-17 00:48:25

包括保存和加载

CheckedListBox与CheckedListBox联动

//查找业务类型
DataTable dtyewu = sb.SelectSyscode(0, true);

if (dtyewu.Rows.Count > 0)
{

flagcbb = false;
clb_yewu.DataSource = dtyewu;
clb_yewu.DisplayMember = "name";
clb_yewu.ValueMember = "id";
DataRowView dr = clb_yewu.Items[0] as DataRowView;
dtyewuleixingbind(dr["id"].ToString(), true);

flagcbb = true;

}

biz = rc.Bizid.Split(';');
pit = rc.Pid.Split(';');
selectcbb();
loadbing();

private void selectcbb()
{
int m = 0;
for (int j = 0; j < clb_yewu.Items.Count; j++)
{
DataRowView dr = clb_yewu.Items[j] as DataRowView;

for (int i = 0; i < biz.Length; i++)
{
if (dr["id"].ToString() == biz[i])
{
clb_yewu.SetItemChecked(j, true);
break;
}
}

}
}

/// <summary>
/// 所有平台信息
/// </summary>
private void selectPingtai()
{
string m;
string[] pt;
DataTable dtapt = sb.SelectAllPingTai();

dtallpt.Columns.Add("id", typeof(Int32));
dtallpt.Columns.Add("cd", typeof(string));
dtallpt.Columns.Add("name", typeof(string));
for (int i = 0; i < dtapt.Rows.Count; i++)
{

m = dtapt.Rows[i][0].ToString();
pt = m.Split(';');
foreach (string ptf in pt)
{
DataRow dr = dtallpt.NewRow();
dr["id"] = dtapt.Rows[i][1].ToString();
dr["cd"] = ptf;
dr["name"] = dtapt.Rows[i][2].ToString();
dtallpt.Rows.Add(dr);
}
}
}

 private void clb_yewu_SelectedIndexChanged(object sender, EventArgs e)
{
if (flagcbb)
{
string cdd = "";
int m = clb_yewu.SelectedIndex;
if (clb_yewu.CheckedItems.IndexOf(clb_yewu.Items[m]) >= 0)
dtyewuleixingbind(clb_yewu.SelectedValue.ToString(), true);
if (clb_yewu.CheckedItems.IndexOf(clb_yewu.Items[m]) <= 0)
{
dtyewuleixingbind(clb_yewu.SelectedValue.ToString(), false);
}
}
}
private void clb_SelectedIndexChanged(object sender, EventArgs e)
{
int m = ((CheckedListBox)sender).SelectedIndex;
if (((CheckedListBox)sender).CheckedItems.IndexOf(((CheckedListBox)sender).Items[m]) >= 0)
{
for (int j = 0; j < clb_yewu.Items.Count; j++)
{ DataRowView dr = clb_yewu.Items[j] as DataRowView;
if (dr["id"].ToString() == ((CheckedListBox)sender).Name)
{
clb_yewu.SetItemChecked(j, true);
break;
}
} } }
/// <summary>
/// 筛选平台
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private DataTable selectdypt(string id)
{
DataRow[] drArr = dtallpt.Select("cd ='" + id + "'");
DataTable dtNew = dtallpt.Clone();
for (int i = 0; i < drArr.Length; i++)
{
dtNew.ImportRow(drArr[i]); }
return dtNew;
}
/// <summary>
/// 绑定平台
/// </summary>
/// <param name="id"></param>
private void dtyewuleixingbind(string id, bool select) //xu
{
//DataTable dtpingtai = sb.SelectPingtai(cdd);
DataTable dtpingtai = selectdypt(id);
bool flag = false;
foreach (Control con in panel2.Controls)
{
if (con.Name == id)
{
con.Visible = true;
flag = true;
if (!select)
{
for (int j = 0; j < ((CheckedListBox)con).Items.Count; j++)
((CheckedListBox)con).SetItemChecked(j, false);
}
}
else
{
con.Visible = false;
} }
if (!flag)
{
CheckedListBox clb_yewu = new CheckedListBox();
clb_yewu.Name = id;
clb_yewu.Visible = true;
if (dtpingtai.Rows.Count > 0)
{
clb_yewu.DataSource = dtpingtai;
clb_yewu.DisplayMember = "name";
clb_yewu.ValueMember = "id";
clb_yewu.SelectedIndexChanged += new EventHandler(clb_SelectedIndexChanged);
}
Size ss = new Size(200, 150);
clb_yewu.Size = ss;
clb_yewu.CheckOnClick = true;
panel2.Controls.Add(clb_yewu); } } #region 修改个人信息平台加载
/// <summary>
/// 平台数据加载
/// </summary>
private void loadbing()
{
if (uid > 0)
{
for (int z = 0; z < biz.Length; z++)
{
DataTable dtpingtai = selectdypt(biz[z]);
CheckedListBox clb_yewu = new CheckedListBox();
clb_yewu.Name = biz[z];
clb_yewu.Visible = true;
if (dtpingtai.Rows.Count > 0)
{
clb_yewu.DataSource = dtpingtai;
clb_yewu.DisplayMember = "name";
clb_yewu.ValueMember = "id";
} Size ss = new Size(200, 150);
clb_yewu.Size = ss;
panel2.Controls.Add(clb_yewu); }
ptbinding();
}
}
/// <summary>
/// 选中对应数据库项
/// </summary>
private void ptbinding()
{
int i = 0; foreach (Control c in panel2.Controls)
{
i = 0;
for (int j = 0; j < ((CheckedListBox)c).Items.Count; j++)
{
DataRowView dr = ((CheckedListBox)c).Items[j] as DataRowView;
if (pit != null)
foreach (string pt in pit)
{
string[] p = pt.Split('-');
if (dr["cd"].ToString() == p[0])
if (dr["id"].ToString() == p[1])
{ ((CheckedListBox)c).SetItemChecked(i, true);
break;
} }
i++; }
}
}