0

How do I find the section of a specified column and row?

I am having a datagridview as a table,as you can see in the picture below.

enter image description here

In the datagridview, the cells are containing rules. (TE',1) means it is the rule TE', and it is the 1st rule.

My task is like I have an expression, such as i+i*i, and using "rules" as showed in the first columns, I find the value of the cell in the datagridview, which's column is the first character of the expression, and which's row is the first character of the second part. I know it sounds nonsense at first, but let me clear this with an example.

The first step is (i+i*i#,E#,empty). I check the first character of the first part, its i, I check the first character of the second part, its E, I look for the section of column i and row E. It is (TE',1), so in the starting step, I replace E for the cell value TE', and add the number 1 for the third part, which was empty at the beginning. Now the actual "formula" is like:

(i+i*i#,TE',1)

I replaced the second part with the "rule" I found at the cell, and added the number of the rule to the third part of the formula.

Next step, I find the section of column i and row T, it is (FT',4), so I replace T with FT', and add the number 4 to the third part.

(i+i*i#, FT'E'#,14)

Now I Look for the section of column i and row F , its (i,8). I do the replace again.

(i+i*i#,iT'E'#,148)

Now for column i and row i its a pop, so i remove both from the formula.

(+i*i#,T'E'#,148)

Now I look for the column + and row T'.......

The whole procedure ends when there are only # and numbers in the formula, like (#,#,1942682).

I filled the datagridview like this (I am also very sorry about this ..):

private void AddRows()
    {
        string[] header_Row = new string[] { "", "+", "*", "(", ")", "i", "#", };
        dataGridView.Rows.Add(header_Row);

        string[] E_Row = new string[] { "E", "", "", "(TÉ,1)", "", "(TÉ,1)", "" };
        dataGridView.Rows.Add(E_Row);

        string[] EComma_Row = new string[] { "É", "(+TÉ,2)", "", "", "(Eps,3)", "", "(Eps,3)" };
        dataGridView.Rows.Add(EComma_Row);

        string[] T_Row = new string[] { "T", "", "", "(FT',4)", "", "(FT',4)", "" };
        dataGridView.Rows.Add(T_Row);

        string[] TComma_Row = new string[] { "T'", "(Eps,6)", "(+FT',5)", "", "(Eps,6)", "", "(Eps,6)" };
        dataGridView.Rows.Add(TComma_Row);

        string[] F_Row = new string[] { "F", "", "", "(E,7)", "", "(i,8)", "" };
        dataGridView.Rows.Add(F_Row);

        string[] emptyRow = new string[] { };
        dataGridView.Rows.Add(emptyRow);

        string[] Plus_Row = new string[] { "+", "pop", "", "", "", "", "" };
        dataGridView.Rows.Add(Plus_Row);

        string[] Star_Row = new string[] { "*", "", "pop", "", "", "", "" };
        dataGridView.Rows.Add(Star_Row);

        string[] StartingBracket_Row = new string[] { "(", "", "", "pop", "", "", "" };
        dataGridView.Rows.Add(StartingBracket_Row);

        string[] EndingBracket_Row = new string[] { ")", "", "", "", "pop", "", "" };
        dataGridView.Rows.Add(EndingBracket_Row);

        string[] i_Row = new string[] { "i", "", "", "", "", "pop", "" };
        dataGridView.Rows.Add(i_Row);

        string[] Hashmark_Row = new string[] { "#", "", "", "", "", "", "Elfogad" };
        dataGridView.Rows.Add(Hashmark_Row);
    }

So the problem is that if I have the first formula (i+i*i#,E#,empty), how do I search for the i column's and E row's section in the datagridview, to get the value of that cell?

dezox
  • 146
  • 9
  • 1
    It is unclear what your question is. To reference an individual cell in the grid may look something like… `datagridview.Rows[Target_Row_Index}.Cells[Target_Column_Index].Value` Obviously you will want to check the `Target_Row_Index` and `Target_Column_Index` to make sure they are within the bounds of the number of rows and columns. – JohnG Nov 24 '19 at 22:16
  • Okay, let me clear this. How do I find the cell that is in the section of column **i** and row **E**? I want to get the value of that cell. – dezox Nov 24 '19 at 22:24
  • Hmmm…. From your picture and code, I am guessing the grid does not have a “Header” (row with column names), therefore, since E is in what appears to be row one (1) and `i` is column five (5). Therefore the intersection would be `datagridview.Rows[1}.Cells[5].Value` – JohnG Nov 24 '19 at 22:32

2 Answers2

1

I am not going to attempt to follow the logic of what your program is doing, but to get to the values of each cell I put together the following example:

This will populate two rows of a grid. When the button is clicked it will loop through the rows and columns of the datagridview and display a message with the indexes and the value of the cell.

private void Form1_Load(object sender, EventArgs e)
{ 
    string[] row1 = new string[] { "A1", "A2", "A3" };
    string[] row2 = new string[] { "B1", "B2", "B3" };

    dataGridView1.Rows.Add(row1);
    dataGridView1.Rows.Add(row2);
    dataGridView1.Refresh();
}

private void button1_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        foreach(DataGridViewColumn col in dataGridView1.Columns)
        {
            string s = string.Format("I am at row {0} , col{1} the value is {2}", row.Index, col.Index, (string)dataGridView1.Rows[row.Index].Cells[col.Index].Value);
            MessageBox.Show(s);
         }
    }
}

The Value of a cell, when accessed will be an object. That is why I have a string cast on the line within the loop. You just need to know the row index and column index. The first column and row start at 0.

Das_Geek
  • 2,775
  • 7
  • 20
  • 26
  • Hey Tim, thank you for your answer, it is helpful. I am now wondering, is there a way to only loop through the first row of the DataGridView? – dezox Nov 24 '19 at 22:34
0

If you want to loop through the first row only, just use 0 as the index. foreach(DataGridViewColumn col in dataGridView1.Columns) string cellValue = (string)dataGridView1.Rows[0].Cells[col.Index].Value)

You could actually write a method, something like what is shown below, it would return a null if you passed in an invalid row or col, otherwise it would return a string value of the cell content. This is assuming all of you data will be strings.

You would then just call it:

  string s = GetCellValue(row, col);


   string GetCellValue(int row, int col)
   {

   try
    {
      return (string)dataGridView1.Rows[row].Cells[col].Value);
    }
    catch
    {
        return null;
    }
   }
  • Welcome to StackOverflow! Rather than posting two answers on the same question, [edit your first answer](https://stackoverflow.com/posts/59023060/edit) to include the updated information of this one. – Das_Geek Nov 25 '19 at 01:00