c# - 将列表框项目转换为 datagridview 复选框列中的选中行
问题描述
我想使数据网格(复选框列)行打勾。数据网格中与列表框项目中的学生代码列匹配的那些行。
我已经尝试了下面的代码。但我没有得到正确的结果
private void btnConvertItemsToCheckedRows_Click(object sender, EventArgs e)
{
if( ListBox1.ListBox.Items.Count>0)
{
for (int i = 0; i <ListBox1.ListBox.Items.Count; i++)
{
foreach (DataGridViewRow row in GridStudents.DataGridView.Rows)
{
if(row.Cells["StudentCode"].Value.ToString().Equals(ListBox1.ListBox.Items[i]))
{
GridStudents.DataGridView.Rows[i].DataGridView["ChekboxColumn", i].Value = true;
}
}
}
}
}
解决方案
试试这个:
private void btnConvertItemsToCheckedRows_Click(object sender, EventArgs e)
{
if( ListBox1.ListBox.Items.Count>0)
{
for (int i = 0; i <ListBox1.ListBox.Items.Count; i++)
{
foreach (DataGridViewRow row in GridStudents.DataGridView.Rows)
{
if(row.Cells["StudentCode"].Value.ToString().Equals(ListBox1.ListBox.Items[i]))
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells["ChekboxColumn"];
chk.TrueValue = true;
chk.Value = chk.TrueValue;
}
}
}
GridStudents.EndEdit();
}
}
推荐阅读
- java - JavaFx 的 setOnAction 方法在底层是如何工作的?
- visual-studio-code - 在启动时隐藏 vscode 快捷方式提示?
- keycloak - 具有不同会话到期长度的客户端的 Keycloak
- java - 在java中的2D int数组中查找int值的位置
- c# - 如何在 SQL 数据库中存储复选框值
- python - Sobel 滤波器的结果很差
- hyperlink - 如何在基于字段的microsoft access中为超链接添加友好名称?
- c# - 将 Button.CommandParameter 绑定到 MVVM 对象以使用相同的 ICommand 到不同的按钮
- django - django 获取上一个网址
- ffmpeg - 如何使用ffmpeg创建一个覆盖到视频中的圆形倒计时指示器?