2 回答

TA貢獻1829條經(jīng)驗 獲得超9個贊
您需要找到控件并將其強制轉(zhuǎn)換為復(fù)選框?qū)ο螅缓蟛榭此欠裉幱谶x中狀態(tài)。該屬性是復(fù)選框存儲真/假值的位置,用于檢查這些值?,F(xiàn)在,您只是在檢查單元格中的數(shù)據(jù),以查看它是否有任何值,并且每次都返回true。我在下面這樣做的方式是如何使用 ASP.NET Webforms GridView對象來執(zhí)行此操作。我認為這與Windows Forms的想法大致相同,只是DataGridView可能有不同的方法來“查找你的控件”,而不是你在GridView的 ASP.NET Webforms中使用的方法。但我敢打賭,情況可能也是一樣的。CheckedFindControl
我四處走動,無法使用Windows Forms測試此確切功能,但是,但是,為了使邏輯正常工作而需要做的事情背后的想法是完全相同的。
將您的狀況更改為如下所示:
foreach (DataGridViewRow row in computerSelection.compGridView.Rows)
{
// Don't cast as bool. Convert to Checkbox object and see if it is checked.
if (((CheckBox)row.FindControl("CheckBox")).Checked) // row.FindControl might be different but might be the same for Winforms.
{
computerSelection.ComputersList.Add(row.Cells[1].Value.ToString());
}
}

TA貢獻1820條經(jīng)驗 獲得超10個贊
我找到了做到這一點的方法!使用 CellContentClick Event,然后使用 EditedFormatedValue 屬性,這次布爾值為 REAL。
private void compGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
if ((bool)compGridView.Rows[e.RowIndex].Cells[0].EditedFormattedValue)
{
ComputersList.Add(compGridView.Rows[e.RowIndex].Cells[1].Value.ToString());
}
else
{
ComputersList.Remove(compGridView.Rows[e.RowIndex].Cells[1].Value.ToString());
}
}
}
謝謝
編輯:弄清楚為什么我一開始就有這個問題,這甚至有點滑稽。我有一個事件,當(dāng)關(guān)閉表單時,每個復(fù)選框的值都會變?yōu)門RUE!
private void ComputerSelection_FormClosed(object sender, FormClosedEventArgs e)
{
for(int i = 0; i < compGridView.Rows.Count; i++)
{
compGridView.Rows[i].Cells[0].Value = true;
}
}
- 2 回答
- 0 關(guān)注
- 139 瀏覽
添加回答
舉報