In winforms Radgridview whenever I need to create a new row I have been generating a class with members for each column like:
public
class
stopCreationRow
{
public
string
shipper, consignee, zip;
}
Then I create and use it for adding rows like:
private
void
radButton35_Click(
object
sender, EventArgs e)
{
stopCreationRow newRow =
new
stopCreationRow();
//throughout the code set the various fields as needed
newRow.consignee =
"person"
;
newRow.shipper =
"new shipper"
;
//then transfer everything to the proper cells
GridViewDataRowInfo dataRowInfo =
new
GridViewDataRowInfo(
this
.stopCreation_grid.MasterView);
dataRowInfo.Cells[
"consignee"
].Value = newRow.consignee;
dataRowInfo.Cells[
"shipper"
].Value = newRow.shipper;
//finally create the row
stopCreation_grid.Rows.Add(dataRowInfo);
}
My question is two fold:
1. Can I somehow use newRow from above to directly create the row?
2. Instead of created my own class, since all this information is already in the datagrid can I access the field names though a class derived from the datagrid?
7 Answers, 1 is accepted
You can pass the values directly to the Add method:
radGridView1.Rows.Add(20,
"test"
,
"test"
, DateTime.Now);
I hope this will be useful. Let me know if you have additional questions.
Regards,
Dimitar
Telerik by Progress
That does not really address the question at all.
I know you can just pass the values directly in, I have 28 columns in this grid alone -- keeping everything in perfect order is not feasible.
What happens if I change the order of the columns?
The only other way to add rows is to use the AddNew method. This method automatically adds a blank row and allows you to set the values:
var row = radGridView1.Rows.AddNew();
row.Cells[
"Name"
].Value =
"test"
;
In addition could you please specify how exactly you expect his to work?
I am looking forward to your reply.
Dimitar
Telerik by Progress
Sorry for the confusion.
I was hoping that if my class matched my grid it would "match up"
ie: newRow.consignee = "person", newRow.stop = "stop";
then do gridview.Rows.Add(newRow);
and assuming the grid has columns named person and stop it would put the data in the right places...
too much?
Damn I wish you could edit a post!
I meant :
assuming the grid has columns named consignee and stop....
There is no way to directly add the business object using the grid's API. What is your data source in this case? For example, if you are using BindingList you can directly add the object to it and the grid will reflect the changes.
Should you have any other questions do not hesitate to ask.
Regards,
Dimitar
Telerik by Progress
Got it Dimitar --
Thanks for the extra data.
Joe