Tell me, please, why can not go into the cycle - the result is empty. In the columns of the letter, in the row of the number, encryption must be performed on the word entered in the textbox ... does not show any errors.

private void button1_Click(object sender, EventArgs e) { string sl = ""; for (int i = 0; i < textBox1.Text.Length; i++) { foreach (DataGridViewColumn col in dataGridView1.Columns) if (col.Name == textBox1.Text[i].ToString()) sl += dataGridView1.CurrentRow.Cells[col.Index].Value; } label1.Text = "Результат: " + sl; } private void Form1_Load(object sender, EventArgs e) { dataGridView1.Rows.Add(2); dataGridView1.Rows[0].Cells[0].Value = 1; dataGridView1.Rows[0].Cells[1].Value = 2; // и т.д. } 
  • no, explain pliz ... columns were added in the properties of the table (Edit columns) - wicS

1 answer 1

And how the condition will be true:

 if (col.Name == textBox1.Text[i].ToString()) 

col.Name and col.HeaderText are different things. Most likely you have col.Name is equal to column1 , column2 and so on, this is automatically generated and is the name to use in the code. And col.HeaderText is A, B, C and so on, and is the name of the column to display on the screen. Use col.HeaderText .

  • thank!!! It works, it helped a lot !!! but if you need several variants of numbers for one letter, then I added another line to the datagrid and for some letters I wrote another number. how to make a condition so that the value is not always taken from the first line? at least an idea) - wicS
  • not quite clear question. Give an example of what you need - krupennikov September
  • Well, for example, in the heading of the table is the letter "A", and under it in the first line is the number 1. If we enter "A" in the textbox, then we will go through the cycle with the number 1. And what if in the same table but already The second line is the number 2 ... Ie. "A" can be replaced by either 1 or 2 ... how then to be? - wicS
  • there are plenty of options. It all depends on the purpose of using the program. As one of the options to use a randomly selected number of the options. The result of entering "AAA" will be approximately the following "221" - krupennikov
  • and how to organize it? put a random loop? and what if somehow to add a condition ...? - wicS