Create function GetAllChechedBox with text and value and insert in new DataTable
-
28-10-2019 - |
문제
I want to take the text and value from selected and not selected elements from CheckBoxList and insert in datatable
MultiCheckCombo Reference
Now I want to get text and value from this checklist, I think it would be comfortable to place in a DataTable
public DataTable GetAllChechedBox()
{
var dt = new DataTable();
for (int i = 0; i < chkList.Items.Count; i++)
{
if (chkList.Items[i].Selected)
{
dt.Columns.Add("Name");
dt.Columns.Add("Value");
// how add all checked with value and text in this datatable?
}
}
return dt;
}
/also want to take a function with text and value for unselected elements/
해결책
Instead of chkList.Items[i].Selected, use chkList.Items[i].Checked....plus add column outside loop
public DataTable GetAllChechedBox()
{
var dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Value");
for (int i = 0; i < chkList.Items.Count; i++)
{
if (chkList.Items[i].Checked)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count-1]["Name"] = chkList.Items[i].Value;
dt.Rows[dt.Rows.Count-1]["Value"] = chkList.Items[i].Text;
}
}
return dt;
}
제휴하지 않습니다 StackOverflow