Guys, I will try to explain this without being too confusing.
I need to add new rows in my datagridview based on a sum output.
Example, I have a textbox and 6 labels that each have a fixed value.
If the entered value in textbox is less than or equal to label1 it adds a new row in datagridview and inserts todays date in row.cells[2] of that new row.
Now, if the value in textbox is greater than label1 value it adds 2 new rows and inserting date value in cells[2] in new rows.
Now it gets tricky, if the textbox value is greater than label1 + label2 value it adds 4 new rows with the date value in cell[2] etc. this need to happen until the value has reached the value on all 6 labels.
is it actually possible to do? the above would fire from a function or button click.
Loading
VulpesPosted Aug 6, 2013, 2:27 PM
mike DelvottiPosted Aug 8, 2013, 7:49 AM
Thank you Vulpes, I slightly strayed from my original question sorry. Thanks for the help.
VulpesPosted Aug 8, 2013, 6:57 AM
row.text = String.Format("{0}.{1}", i + 1, this.Controls["label" + (i + 1).ToString()].Text);
mike DelvottiPosted Aug 8, 2013, 6:35 AM
"Actually reading it again, I don't think you do mean that - you want each label's Text property to appear in a separate row."
There won't be any more than 6 new rows added as the rows added can only be 6 due to the earlier sum process we done if that makes sense?
I'm using a resource scheduler, there's only 6 rows on it for each day and the rows are driven by the row: row.resindex which only goes up to 6.
VulpesPosted Aug 8, 2013, 6:01 AM
row.text = String.Format("1.{0}\r\n2.{1}\r\n3.{2}\r\n4.{3}\r\n5.{4}\r\n6.{5}", label1.Text, label2.Text, label3.Text, label4.Text, label5.Text, label6.text);
EDIT:
Actually reading it again, I don't think you do mean that - you want each label's Text property to appear in a separate row.
Bearing in mind there are 6 labels, what happens if there are more than 6 new lines added?
mike DelvottiPosted Aug 8, 2013, 5:53 AM
It's adding all the label values in every new row like belwow:
1. 8640,8640,8640,8640
2.8640,8640,8640,8640
could it be so each row only contains one label value like below, i.e 1, would label1 value, 2 would be label 2 value and so on:
1.8640
2.8640
3.8640
Thanks.
VulpesPosted Aug 8, 2013, 4:38 AM
Also you might want to include a divider between the 'i' and label1.Text.
Something like this perhaps:
row.text = String.Format("{0}:{1},{2},{3},{4},{5},{6}", i + 1, label1.Text, label2.Text, label3.Text, label4.Text, label5.Text, label6.text);
mike DelvottiPosted Aug 8, 2013, 4:20 AM
I want to do the same with row.text, to insert the label values on the form, would it be something like the below?
row.text = (i + label1.Text, label2.Text, label3.Text, label4.Text, label5.Text, label6.text)
VulpesPosted Aug 7, 2013, 1:36 PM
row.resindex = "1";
to this:
row.resindex = (i + 1).ToString();
mike DelvottiPosted Aug 7, 2013, 11:57 AM
Awsome, thank you so much, I got it working using the below in the end:
for (int i = 0; i < num; i++)
{
DataSet.Table1Row row = DataSet.Table1.NewTable1Row();
DataSet.Table1.AddTable1Row(row);
row.start = dateTimePicker1.Value.ToString("dd/MM/yyyy HH:mm");
row.end = dateTimePicker2.Value.ToString("dd/MM/yyyy HH:mm");
row.text = "test";
row.resindex = "1";
}
I have one last cheeky question ;-)
It adds the new rows perfect with your example based on the sum, but you see my 'row.resindex' all the new rows get added with "1" is there a way I could step up so the 2nd new row would insert 'row.resindex = "2" the "3" and so on. if there's 3 new rows added of course.
Thank you.
VulpesPosted Aug 7, 2013, 6:35 AM
dataGridView1.Rows.Add(num);
Are you sure that you have i-- rather than i++ in the 'for' statement?
mike DelvottiPosted Aug 7, 2013, 6:22 AM
DataGridView.Rows[DataGridView.Rows.Count - i].Cells[2].Value = DateTime.Today.ToString();
But I get the error on the above line at the 'Value' part:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
VulpesPosted Aug 7, 2013, 5:25 AM
mike DelvottiPosted Aug 7, 2013, 4:21 AM
I forgot to mention my datagridview fills and updates via a table adaptor, I'm getting the below error thrown:
'Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
'
On this line:
table1DataGridView.Rows.Add(num);