第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

DataGridView 中的復(fù)選框值始終為 true

DataGridView 中的復(fù)選框值始終為 true

C#
青春有我 2022-08-20 16:03:22
我有一個WinForm應(yīng)用程序,它具有DataGridView,其中一列帶有CheckBox。使用復(fù)選框選擇幾行后,我需要迭代行并檢查復(fù)選框是否被勾選。我已經(jīng)嘗試過 woth for 和 foreach 循環(huán),但每次 bx 的值都是真實的!有趣的是,我有一個按鈕可以選擇全部,另一個按鈕使用代碼清除所有內(nèi)容,并且它有效!我用于創(chuàng)建 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");迭代代碼(我搜索的大多數(shù)帖子都將該解決方案共享為正確的解決方案): foreach (DataGridViewRow row in computerSelection.compGridView.Rows)            {                if ((bool)row.Cells["CheckBox"].Value)                {                    computerSelection.ComputersList.Add(row.Cells[1].Value.ToString());                }            }此代碼始終返回 TRUE,即使在未選中的復(fù)選框上也是如此。我在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條經(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());

            }

        }


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

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;

            }

        }


查看完整回答
反對 回復(fù) 2022-08-20
  • 2 回答
  • 0 關(guān)注
  • 139 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號