[解决]如何从Gridview数据控制CheckboxList。 [英] [Solved] How to Control CheckboxList from Gridview Data.
问题描述
嗨开发者,
我面临以下问题:将gridview某些列数据与checkboxlist项目文本进行比较。
我有一个只有6个项目的复选框。
A
B
C
D
E
F
当从数据库和第3列填充gridview时,即:Cell [2] .Text具有字符串值A,B,C,D,E或F.每当它被填充时,我必须禁用该复选框列表中具有相同值的项目,如gridview cell [2]。
例如,如果我在Gridview单元格[2]中有A,C,E,F,那么Checkboxlist项目A,C,E,F应为enable = false ;
因为我为Page.Load事件创建了以下方法。
Hi Developers,
I am facing the following issue regarding comparing my gridview certain column data with checkboxlist item text.
I have a checkboxlist which has only 6 items.
A
B
C
D
E
F
When the gridview is populated from database and the 3rd column i-e: Cell[2].Text has the string values of A, B, C, D, E or F. Whenever it is being populated I have to disable the items of that checkboxlist which has same values like gridview cell[2].
For example if I have A, C, E, F in Gridview cell[2] then the Checkboxlist item A, C, E, F should be enable = false;
as I have created the following method for Page.Load Event.
private void EditMode()
{
foreach(GridViewRow gvRow in GridView3.Rows)
{
Label userID = gvRow.Cells[2].FindControl("Label4") as Label;
for (int k = 0; k < CheckBoxList1.Items.Count; k++)
{
if (CheckBoxList1.Items[k].Text == userID.Text)
{
CheckBoxList1.Items[k].Enabled = false;
}
}
}
}
请帮助..:)
Please Help.. :)
推荐答案
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState != DataControlRowState.Edit) // check for RowState
{
if (e.Row.RowType == DataControlRowType.DataRow) //check for RowType
{
foreach (ListItem c in CheckBoxList1.Items)
{
if (c.Text == userID.Text)
{
c.Enabled = false;
}
}
}
}
}
和保留复选框状态,无论是否已选中
and to retain the checkboxes statuses whether checked or not
protected void GridView3_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable dt2 = ViewState["DatainEditMode"] as DataTable;
int index = Convert.ToInt32(e.RowIndex);
if (dt2.Rows[index][2].ToString() == CheckBoxList1.Items[0].Text.ToString())
{
CheckBoxList1.Items[0].Enabled = true;
}
else if (dt2.Rows[index][2].ToString() == CheckBoxList1.Items[1].Text.ToString())
{
CheckBoxList1.Items[1].Enabled = true;
}
else if (dt2.Rows[index][2].ToString() == CheckBoxList1.Items[2].Text.ToString())
{
CheckBoxList1.Items[2].Enabled = true;
}
else if (dt2.Rows[index][2].ToString() == CheckBoxList1.Items[3].Text.ToString())
{
CheckBoxList1.Items[3].Enabled = true;
}
else if (dt2.Rows[index][2].ToString() == CheckBoxList1.Items[4].Text.ToString())
{
CheckBoxList1.Items[4].Enabled = true;
}
else if (dt2.Rows[index][2].ToString() == CheckBoxList1.Items[5].Text.ToString())
{
CheckBoxList1.Items[5].Enabled = true;
}
dt2.Rows[index].Delete();
string task_no = ((Label)GridView3.Rows[e.RowIndex].FindControl("LBL_TASKNO")).Text;
tskctrl.deleteTaskNoDetail(task_no);
GV3DataBinding(lbl_pTask.Text.ToString());
}
这篇关于[解决]如何从Gridview数据控制CheckboxList。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!