Carlos
Carlos

Reputation: 105

Datagridview - selete a column to populate

Hy guy's

I'm writing a C# code to create 1000 random numbers, I've the code, I know isn't the perfect code, but it's working, ;), My doubt is to achieve the way to populate a specific column in datagridview.

here the code:

        private void GenerateBtn_Click(object sender, EventArgs e)
    {
        Random rnd = new Random();
        string newLine = Environment.NewLine;
        int nums = rnd.Next(100, 1000);
        dataGridView1.Text = nums.ToString();
        for(int i = 1; i <= 100; i++)
        {
            nums = rnd.Next(100, 1000);
            dataGridView1.Text = dataGridView1.Text + newLine + nums.ToString();
        }

and here a datagridview image enter image description here

The goal it's to populate the "PinCode" column with the random number generated by the code until the first column are filled.

Thanks in advance

Best regards

Carlos

Upvotes: 0

Views: 80

Answers (1)

Tys
Tys

Reputation: 3610

Perhaps you should take some time to learn a bit more about how to work with datagridview's, data, rows etc. Looking at your question it seems that going over a few of those tutorials might help a lot to get a better understanding of all this.

But, as for your question / example, you should work with a loop like this:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    //do something like row.Cells["Name or number"].Value = random number;
}

And, even better would be to create a data set which you populate with your data, and bind that to the DataGridView.

Upvotes: 1

Related Questions