I have an issue with some code im writing. I have a Datagridview object that always has two columns, but could have any amount of rows depending on many different scenarios.

I would like to be able to take the values from the DatagridView and put them into an array, or something useful, so that I may use the values stored there to build an XML file later on.

Can I please get some help?

Thanks!

Recommended Answers

All 3 Replies

Hi!

Please check this:

List<string> Col1 = new List<string>();
List<string> Col2 = new List<string>();
DataGridView1.AllowUserToAddRows = false; //removes last(Extra) row
foreach (DataGridViewRow row in DataGridView1.Rows) {
	Col1.Add(row.Cells(0).Value.ToString);
	Col2.Add(row.Cells(1).Value.ToString);
}
DataGridView1.AllowUserToAddRows = true; //add row again If addition of rows allowed.

Hi!

Please check this:

List<string> Col1 = new List<string>();
List<string> Col2 = new List<string>();
DataGridView1.AllowUserToAddRows = false; //removes last(Extra) row
foreach (DataGridViewRow row in DataGridView1.Rows) {
	Col1.Add(row.Cells(0).Value.ToString);
	Col2.Add(row.Cells(1).Value.ToString);
}
DataGridView1.AllowUserToAddRows = true; //add row again If addition of rows allowed.

I think that will do it. I'll check it out shortly. In the mean time from me posting initially, I was able to make it work in a very un graceful manner, basically there is no logic to iterate over the rows. In my half way fix I created today, I allowed for the max of twelve rows to be allowed, which should be sufficient.

Once im done with all of that, I will test this out. thanks for the reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.