This is a migrated thread and some comments may be shown as answers.

use existing grid columns as a class

7 Answers 47 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 2
Joe asked on 23 Mar 2017, 08:28 PM

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

Sort by
0
Dimitar
Telerik team
answered on 24 Mar 2017, 12:10 PM
Hello Joe,

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
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Joe
Top achievements
Rank 2
answered on 24 Mar 2017, 01:41 PM

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?

0
Dimitar
Telerik team
answered on 24 Mar 2017, 02:45 PM
Hello Joe,

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.
 
Regards,
Dimitar
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Joe
Top achievements
Rank 2
answered on 24 Mar 2017, 04:36 PM

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?

0
Joe
Top achievements
Rank 2
answered on 24 Mar 2017, 04:36 PM

Damn I wish you could edit a post!

I meant :

assuming the grid has columns named consignee and stop....

0
Accepted
Dimitar
Telerik team
answered on 27 Mar 2017, 09:17 AM
Hi Joe,

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
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Joe
Top achievements
Rank 2
answered on 28 Mar 2017, 12:03 PM

Got it Dimitar --

 

Thanks for the extra data.

 

Joe

Tags
GridView
Asked by
Joe
Top achievements
Rank 2
Answers by
Dimitar
Telerik team
Joe
Top achievements
Rank 2
Share this question
or