- 1 year ago
How to insert a string into dataGridView?
To insert a string into a DataGridView
in C#, you can use the Rows
property of the DataGridView
control to add a new row and then set the value of the cell in the desired column to the string value. Here's an example:
csharpstring myString = "Hello, world!";
// Add a new row to the DataGridView
dataGridView1.Rows.Add();
// Set the value of the cell in the first column to the string
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0].Value = myString;
In this example, we first create a string called myString
with the value "Hello, world!". We then add a new row to the DataGridView
using the Add()
method of the Rows
property. Finally, we set the value of the cell in the first column of the newly added row to myString
using the Cells
property of the DataGridViewRow
object.
You can customize this code to set the value of the cell in a different column or to add multiple strings to the DataGridView
by repeating the Rows.Add()
and Cells.Value
lines with different values as needed.