1

I have a little problem with iterating through a DataGridView.

Right now I have a textfile (just a long string: true;false;true etc.).

Now I want to read this file and set the value of my CheckBoxColumn in my DataGridView: First value in file = status of first CheckBoxCell Second Value in file = status of second CheckBoxCell and so on

There's no problem in parsing the string but I don't get how to iterate through the cells and set values.

Can anyone help?

Greetings

EDIT: Used OhBeWise's answer and do now have following code snippet:

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";
                }

            }
        }

But now it sets all checkbox values to false, even if there are only the first five false and all others are true. Any ideas?

EDIT 2: Simple mistake: I do save 'True' instead of 'true' :D Works like a charm. Thank you

2 Answers2

0

One way :

// 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

Bioukh
  • 1,888
  • 1
  • 16
  • 27
0

Here is a simple solution with two examples:

  1. DataGridView.DataSource is not bound, so no rows exist yet.
  2. DataGridView.DataSource is bound, so rows do exist.

No Binding Example

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);
    }
  }
}

Binding Example

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";
    }
  }
}

Common Ground

In both examples, call method as follows:

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

This is a working example. Hope this helps.

Edit

Do note, if you are binding to a DataSource object, it would be more fitting to just add a boolean property to the object and load the values into the list of objects before binding.

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;
}

Then, you wouldn't need AddCheckColumn() nor Form1_Load.

OhBeWise
  • 5,350
  • 3
  • 32
  • 60