1

我在遍历 DataGridView 时遇到了一点问题。

现在我有一个文本文件(只是一个长字符串:true;false;true 等)。

现在我想读取这个文件并在我的 DataGridView 中设置我的 CheckBoxColumn 的值:文件中的第一个值 = 第一个 CheckBoxCell 的状态文件中的第二个值 = 第二个 CheckBoxCell 的状态等等

解析字符串没有问题,但我不知道如何遍历单元格并设置值。

任何人都可以帮忙吗?

问候

编辑: 使用 OhBeWise 的答案,现在有以下代码片段:

private void Btn_LoadChampions_Click(object sender, EventArgs e)
        {
            string allChampionStates = null;

            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Title = "Open Text File";
            openFileDialog1.Filter = "CSV|*.csv";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                allChampionStates = File.ReadAllText(openFileDialog1.FileName);               
                List<string> vals = allChampionStates.TrimEnd(';').Split(';').ToList();

                int maxRows = Math.Min(this.dataGridView1.Rows.Count, vals.Count);

                for (int i = 0; i < maxRows; i++)
                {
                    DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)this.dataGridView1.Rows[i].Cells["Status"];
                    cell.Value = vals[i] == "true";
                }

            }
        }

但是现在它将所有复选框的值设置为 false,即使只有前五个为 false 而其他所有为 true。有任何想法吗?

编辑 2: 简单的错误:我确实保存了 'True' 而不是 'true' :D 像魅力一样工作。谢谢

4

2 回答 2

0

单程 :

// Retrieve the cell value for the cell at column 3, row 7.
String testValue1 = (String)dataGridView1[3, 7].Value;

https://msdn.microsoft.com/fr-fr/library/ms158656(v=vs.110).aspx

于 2015-03-19T15:38:34.660 回答
0

这是一个带有两个示例的简单解决方案:

  1. DataGridView.DataSource未绑定,因此尚不存在行。
  2. DataGridView.DataSource是绑定的,所以行确实存在。

无绑定示例

private void AddCheckColumn()
{
  DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
  col.Name = "Checked";

  if (!this.dataGridView1.Columns.Contains(col.Name))
  {
    this.dataGridView1.Columns.Add(col);

    string values = "true;false;true;false;";
    List<string> vals = values.TrimEnd(';').Split(';').ToList();

    foreach (string val in vals)
    {
      DataGridViewRow row = new DataGridViewRow();
      row.CreateCells(this.dataGridView1);

      DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)row.Cells[0];
      cell.Value = val == "true";
      this.dataGridView1.Rows.Add(row);
    }
  }
}

绑定示例

public class Example
{
  public string Foo { get; set; }
  public string Bar { get; set; }
}

public Form1()
{
  InitializeComponent();
  this.AddDataSource();
}

public BindingList<Example> Examples { get; set; }

private void AddDataSource()
{
  this.Examples = new BindingList<Example>()
  {
    new Example() { Bar = "Bar 1", Foo = "Foo 1"},
    new Example() { Bar = "Bar 2", Foo = "Foo 2"},
    new Example() { Bar = "Bar 3", Foo = "Foo 3"},
    new Example() { Bar = "Bar 4", Foo = "Foo 4"},
  };

  this.dataGridView1.DataSource = this.Examples;
}

private void AddCheckColumn()
{
  DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
  col.Name = "Checked";

  if (!this.dataGridView1.Columns.Contains(col))
  {
    this.dataGridView1.Columns.Add(col);

    string values = "true;false;true;false;";
    List<string> vals = values.TrimEnd(';').Split(';').ToList();

    int maxRows = Math.Min(this.dataGridView1.Rows.Count, vals.Count);

    for (int i = 0; i < maxRows; i++)
    {
      DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)this.dataGridView1.Rows[i].Cells["Checked"];
      cell.Value = vals[i] == "true";
    }
  }
}

共同点

在这两个示例中,调用方法如下:

private void Form1_Load(object sender, EventArgs e)
{
  this.AddCheckColumn();
}

这是一个工作示例。希望这可以帮助。

编辑

请注意,如果您绑定到一个DataSource对象,那么在绑定之前向对象添加一个布尔属性并将值加载到对象列表中会更合适。

private void AddDataSource()
{
  this.Examples = new BindingList<Example>()
  {
    new Example() { Bar = "Bar 1", Foo = "Foo 1"},
    new Example() { Bar = "Bar 2", Foo = "Foo 2"},
    new Example() { Bar = "Bar 3", Foo = "Foo 3"},
    new Example() { Bar = "Bar 4", Foo = "Foo 4"},
  };

  string values = "true;false;true;false;";
  List<string> vals = values.TrimEnd(';').Split(';').ToList();
  int maxRows = Math.Min(this.Examples.Count, vals.Count);

  for (int i = 0; i < maxRows; i++)
  {
    this.Examples[i].Checked = vals[i] == "true";
  }

  this.dataGridView1.DataSource = this.Examples;
}

然后,您将不需要AddCheckColumn()也不Form1_Load.

于 2015-03-19T16:56:32.800 回答