Reputation: 267
I ran into a problem that I've been banging my head on for weeks...
I've came up with a solution that is terrible and extremely long and embarrassing.
Now I wonder if I can get some professional help from you :)
I'm trying to loop through a datagridview and set a order for for my images.
My in-data is a datagridview that has two columns (id, sortorder)
The problem is that my in-data sortorder is 1 for first image and 2 for last image and allot of value 0 that should be in between So I want to convert these values:
id sortorder 9520 1 9520 0 9520 0 9520 0 9520 0 9520 2
To this:
id sortorder 9520 1 (original was 1) 9520 2 9520 3 9520 4 9520 5 9520 6 (original was 2)
EDIT: The code I got so far is this:
void setSort()
{
dgv2.ColumnCount = 5;
dgv2.Columns[0].Name = "PropertyID";
dgv2.Columns[1].Name = "Type";
dgv2.Columns[2].Name = "Status";
dgv2.Columns[3].Name = "ImageName";
dgv2.Columns[4].Name = "Order";
string id = dgv1[0, 0].Value.ToString();
int sOrder = 0;
foreach (DataGridViewRow row in dgv1.Rows)
{
if (row.Cells["PropertyID"].Value == null) break;
string id2 = row.Cells["PropertyID"].Value.ToString();
if (id2 != id) sOrder = 0;
sOrder++;
row.Cells["Type"].Value = sOrder;
id = id2;
dgv2.Rows.Add(row.Cells[0].Value.ToString(), row.Cells[2].Value.ToString(), row.Cells[3].Value.ToString(), row.Cells[0].Value.ToString() + "-" + id + "." + row.Cells[4].Value.ToString(), id);
}
}
Upvotes: 0
Views: 71
Reputation: 267
I managed to make the changes needed in the in-data instead, so now I'm able to use TaW's solution as it is :) Thank you very much for all your assistance and patience
Upvotes: 0
Reputation: 54433
Would this do the job?
void setSort()
{
string id = dataGridView1[0, 0].Value.ToString();
int sOrder = 0;
for (int row = 0; row < dataGridView1.Rows.Count; row++)
{
if (dataGridView1[0, row].Value == null) break;
string id2 = dataGridView1[0, row].Value.ToString();
if (id2 != id) sOrder = 0;
sOrder++;
dataGridView1[1, row].Value = sOrder;
id = id2;
}
}
Upvotes: 1