亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

DataGridView 中的復選框值始終為 true

DataGridView 中的復選框值始終為 true

C#
青春有我 2022-08-20 16:03:22
我有一個WinForm應用程序,它具有DataGridView,其中一列帶有CheckBox。使用復選框選擇幾行后,我需要迭代行并檢查復選框是否被勾選。我已經嘗試過 woth for 和 foreach 循環,但每次 bx 的值都是真實的!有趣的是,我有一個按鈕可以選擇全部,另一個按鈕使用代碼清除所有內容,并且它有效!我用于創建 DataGridView 的代碼: ComputerSelection computerSelection = new ComputerSelection();            DataGridViewCheckBoxColumn checkBox = new DataGridViewCheckBoxColumn();            checkBox.ValueType = typeof(bool);            checkBox.Name = "CheckBox";            checkBox.HeaderText = "Select";            computerSelection.compGridView.Columns.Add(checkBox);            computerSelection.compGridView.Columns.Add("Name", "Name");            computerSelection.compGridView.Columns.Add("AgentVersion", "Agent Version");            computerSelection.compGridView.Columns.Add("Status", "Status");            computerSelection.compGridView.Columns.Add("Domain", "Domain");迭代代碼(我搜索的大多數帖子都將該解決方案共享為正確的解決方案): foreach (DataGridViewRow row in computerSelection.compGridView.Rows)            {                if ((bool)row.Cells["CheckBox"].Value)                {                    computerSelection.ComputersList.Add(row.Cells[1].Value.ToString());                }            }此代碼始終返回 TRUE,即使在未選中的復選框上也是如此。我在StackOverFlow上搜索了這么多帖子,甚至試圖使用DataGridViewCheckBoxCell,但沒有成功。選擇所有按鈕代碼(使用相同的機制:(): private void SelectAllButton_Click(object sender, EventArgs e)        {            for (int i = 0; i < compGridView.Rows.Count; i++)            {                compGridView.Rows[i].Cells[0].Value = true;            }        }我需要在每次迭代后“行”。細胞[1]。Value.ToString()“ 代碼將返回 false 或 true,并不總是 true。
查看完整描述

2 回答

?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

您需要找到控件并將其強制轉換為復選框對象,然后查看它是否處于選中狀態。該屬性是復選框存儲真/假值的位置,用于檢查這些值?,F在,您只是在檢查單元格中的數據,以查看它是否有任何值,并且每次都返回true。我在下面這樣做的方式是如何使用 ASP.NET Webforms GridView對象來執行此操作。我認為這與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());

            }

        }


查看完整回答
反對 回復 2022-08-20
?
拉莫斯之舞

TA貢獻1820條經驗 獲得超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());

                }

            }

        }

謝謝


編輯:弄清楚為什么我一開始就有這個問題,這甚至有點滑稽。我有一個事件,當關閉表單時,每個復選框的值都會變為TRUE!


 private void ComputerSelection_FormClosed(object sender, FormClosedEventArgs e)

        {

            for(int i = 0; i < compGridView.Rows.Count; i++)

            {

                compGridView.Rows[i].Cells[0].Value = true;

            }

        }


查看完整回答
反對 回復 2022-08-20
  • 2 回答
  • 0 關注
  • 145 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號