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

Add New Row - Winforms GridView Event

33 Answers 2129 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mo
Top achievements
Rank 1
Mo asked on 27 Feb 2008, 04:08 PM
Hello, this is probably a really simple question, but I can't figure it out right now.  I'm using the Winforms GridView control, and I want to be able to add a new row within the gridview control.  I do have "AllowAddNewRow" set to true.  Which event is being initiated when you add a new row to the grid?  I easily was able to use the "RowDeleting" event when I delete a row, so I figure that there would be a "RowAdding" or a "RowAdded" event.  But, there wasn't.  I simply want to Add/Update/Delete rows.  It's pretty simple with the ASP.NET control with the Insert,Update,Delete command events, but I can't seem to find anything similar in the winforms control.  So, which event is used to Update a row, and which event is used to Add a new row (I want to be able to add the new row using the "click here to add new row" area), or what is the process to implement these 2 events? 

I'm using the Q3 2007 V5 version.

thanks,
monirak

33 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 27 Feb 2008, 04:46 PM
Hello Mo,

Thank you for contacting us.

Unfortunately, RadGridView doesn't support the RowAdding and the RowAdded events at this time. We will add such events in our upcoming release Q1 2008. I suggest using the ListChanged event of your currency manager.

When a value in some cell is changed, the ValueChanged event of the RadGridView is fired.

Let us know, if you have other questions.

All the best,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Mo
Top achievements
Rank 1
answered on 11 Mar 2008, 06:11 PM
thanks for responding Jack.  So how would you differentiate between an insert and an update within the grid when using ValueChanged or any other grid event with your radGrid?

thanks,
Monirak
0
Jack
Telerik team
answered on 12 Mar 2008, 02:55 PM
Hi Mo,

Thank you for this question.

The ValueChanged event is only fired when a value is changed. You should process this event, if you wish to track updates in RadGridView.

There is no specific event in RadGridView, that is fired when a row is inserted. As I mentioned in the previous post, we will correct this in our upcoming release Q1 2008. You can work around the issue by processing the ListChanged event. Refer to the code snippet below:

CurrencyManager manager = this.radGridView1.BindingContext[this.radGridView1.dataSource] as CurrencyManager;  
manager.ListChanged +=new ListChangedEventHandler(manager_ListChanged);  
void manager_ListChanged(object sender, ListChangedEventArgs e)  
{  
    if (e.ListChangedType == ListChangedType.ItemAdded)  
    {  
        // ...  
    }  
}  
 

I hope this helps. Do not hesitate to write me, if you need further assistance.

Best wishes,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Ferry
Top achievements
Rank 1
answered on 01 Oct 2008, 11:14 AM
I'm using RadControls for WinForms Q2 2008 SP1. In this version I'm still not able to use an event like RowAdding or RowAdded. In wich version will this be possible?
0
Jack
Telerik team
answered on 01 Oct 2008, 04:18 PM
Hello Ferry v.d. Graaf,

Thank you for getting back to us.

You should use the RowsChanging and RowsChanged events for this purpose. The Action property of the event arguments contains information whether the row was added or removed, and the NewItems array contains the affected rows.

Should you have any questions, I will gladly answer them.
 

All the best,
Jack
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
hugo
Top achievements
Rank 2
answered on 03 Dec 2008, 04:21 PM
Hi. my name is hugo.
Im having a similar problem when updating a radgridview value in a cell.
I'm using the latest package (WinForms Q3 2008). I set the grid to allow updating, it lets me change the value on the grid but when I check the db the value is not changed.
Do you have some code showing in VB how to change the values on the grid and post them back to the DB.
Thanks in advance
0
Jack
Telerik team
answered on 04 Dec 2008, 07:47 PM
Hi hugo,

You should call the Update method of your table adapter. Consider the following code:

Me.employeesTableAdapter.Update(Me.nwindDataSet.Employees) 

I hope that helps. Do not hesitate to write us if you need further assistance.

Regards,
Jack
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
hugo
Top achievements
Rank 2
answered on 31 Dec 2008, 06:38 PM
it did. thxs a lot.
0
Ferry
Top achievements
Rank 1
answered on 20 Nov 2009, 10:28 AM
If I use the RowChanging or RowChanged event, I don't get the new cell values. I try to insert a new row, direct after inserting I need the cell values. The NewItems array only contains "Nothing" values.
0
Julian Benkov
Telerik team
answered on 25 Nov 2009, 02:37 PM
Hello Ferry v.d. Graaf,

Please try to access the row from the associated GridViewTemplate:

void radGridView1_RowsChanged(object sender, GridViewCollectionChangedEventArgs e)
{
    object val = e.GridViewTemplate.Rows[e.NewStartingIndex].Cells[0].Value;
}

Greetings,
Julian Benkov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
shuyin ngo
Top achievements
Rank 1
answered on 31 Mar 2010, 09:03 AM
My name's Alvin,
I still didn't get it. After reading the above comments, i got a mess inside my head. I'm new to Telerik and Im using the trial version of Telerik Q1 2010.
Is there another way to add a new row into our DB using the "Click here to add a new row" and the CreateRow event? :"(
0
Julian Benkov
Telerik team
answered on 02 Apr 2010, 04:01 PM
Hello shuyin ngo,
 
You can bind a DataTable object to RadGridView control. After that you can insert/edit/remove rows from the grid which will be automatically refreshed when bound to a DataTable object.

For database updates you must use the ADO.NET DataAdapter object. More information how to use ADO.NET objects for database operations you can find in this online tutorial.
 
Sincerely yours, Julian Benkov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Chad Hensley
Top achievements
Rank 1
answered on 17 Apr 2010, 11:22 PM
I think the problem could be occurring because some are using the SQL CE database and the database changes are not saved from build to build.

I answered my that question here.

http://social.msdn.microsoft.com/Forums/en/sqlce/thread/a4bae5d5-f8bb-44a9-921c-ea817193ae01
0
Rakesh
Top achievements
Rank 1
answered on 03 Aug 2010, 02:01 PM
I am not able to update/insert/delete the data of database table which is bounded to a radgridview by selecting the Data Source for it.
The changes which i make in the gridview, i want that to be reflected in the database table automatically or on a button click, which i could not so please help me and give me code for it in c#.
The database I am using is Sql. 
0
Julian Benkov
Telerik team
answered on 06 Aug 2010, 03:53 PM
Hello Rakesh,

Please view the answer in your next post.

Kind regards,
Julian Benkov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
John Davis
Top achievements
Rank 2
answered on 17 Mar 2011, 02:14 PM
how do you set a column as read-only for updating but not read-only for inserting?
0
Richard Slade
Top achievements
Rank 2
answered on 17 Mar 2011, 05:05 PM
Hello John,

You can control adding, editing and deleting rows in the grid in the following way
this.radGridView1.AllowEditRow = false;
this.radGridView1.AllowAddNewRow = true;
this.radGridView1.AllowDeleteRow = false;
this.radGridView1.AddNewRowPosition = Telerik.WinControls.UI.SystemRowPosition.Top;

Hope that helps
Richard
0
John Davis
Top achievements
Rank 2
answered on 17 Mar 2011, 05:30 PM
Richard,
Thanks. But I do not see how your code could help because it pertains to a whole row.  I ihave a column Userid that is a primary key.
So when the user UPDATES, this Userid column should be read-only, and the user can change only the other columns.
When the user INSERTS, then NONE of the columns should be read-only. 
John
0
Richard Slade
Top achievements
Rank 2
answered on 17 Mar 2011, 05:37 PM
Hello John,

To clarify, is this scenario correct?

1: For editing, you would like the Id column to be read only. The rest of the columns should be editable
2: When adding a new row, you would like all columns to be editable

Thanks
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 17 Mar 2011, 05:46 PM
John,

Hopefully, my summary was correct. If so, then here is a full exmaple that should help.

Designer File
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components;
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
        ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
        this.SuspendLayout();
        // 
        // radGridView1
        // 
        this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.radGridView1.Location = new System.Drawing.Point(0, 0);
        this.radGridView1.Name = "radGridView1";
        this.radGridView1.Size = new System.Drawing.Size(430, 300);
        this.radGridView1.TabIndex = 0;
        this.radGridView1.Text = "radGridView1";
        this.radGridView1.CellClick += new Telerik.WinControls.UI.GridViewCellEventHandler(this.radGridView1_CellClick);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(430, 300);
        this.Controls.Add(this.radGridView1);
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
        this.ResumeLayout(false);
    }
    #endregion
    private Telerik.WinControls.UI.RadGridView radGridView1;
}

Form1.cs
using System.Collections.Generic;
using System.Windows.Forms;
  
  
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
  
            this.radGridView1.AutoGenerateColumns = true;
  
            List<Person> people = new List<Person>();
            people.Add(new Person("Richard", 1));
            this.radGridView1.DataSource = people;
  
            this.radGridView1.Columns["Id"].ReadOnly = true;
  
        }
  
        private void radGridView1_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 && e.Column.Name == "Id")
            {
                this.radGridView1.Columns["Id"].ReadOnly = false;
            }
            else
            {
                this.radGridView1.Columns["Id"].ReadOnly = true;
            }
        }
    }
  
    public class Person
    {
        public Person(string name, int id)
        {
            this.Name = name;
            this.Id = id;
        }
        public Person()
        {
        }
  
        public string Name
        { get; set; }
  
        public int Id
        { get; set; }
    }

Hope that helps, but let me know if you have further questions
Richard
0
John Davis
Top achievements
Rank 2
answered on 17 Mar 2011, 10:03 PM
Richard, This technique works perfectly.  I really appreciate your posting it.
It seems this is a very common scenario, but it is not obvious how to do it, so your post will probably benefit many others.
John
0
Richard Slade
Top achievements
Rank 2
answered on 17 Mar 2011, 10:09 PM
You're welcome. Glad I could help. though I will go over it again when I have a moment, as I've realised that this doesn't account for tabbing through the items. For exmaple, if you are on the new row section and are editing a cell, ready to add, you cannot tab into the read only cell. Instead you must click on it. I'll let you know a workaround for that as soon as I can
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 17 Mar 2011, 10:29 PM
There you go.. This is much easier and works for both tabbing and click

this.radGridView1.CellBeginEdit += new Telerik.WinControls.UI.GridViewCellCancelEventHandler(this.radGridView1_CellBeginEdit);   
  
private void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
    {
        if (e.RowIndex > -1 && e.Column.Name == "Id")
        {
            e.Cancel = true;
        }
    }

Best regards,
Richard
0
John Davis
Top achievements
Rank 2
answered on 18 Mar 2011, 02:00 AM
Richard, As you said, your most recently posted technique works even better.  I found no problems with it.
John
0
Stephen Breytenbach
Top achievements
Rank 1
answered on 20 Apr 2011, 10:23 AM
Hi Richard,
Using WinForms, Q1, 2011
I am attempting the same solution, I have a load on Demand grid, master and child, the Child expands with data correctly, when i click on the Add New Row part, (or any where in the child for that matter), my RowIndex is always -1, 

private void radGridView_CellBeginEdit_1(object sender, GridViewCellCancelEventArgs e)
 {
     if (e.RowIndex > -1 && e.Column.Name == "Barcode")
     {
         e.Cancel = true;
     }
     if (e.RowIndex > -1 && e.Column.Name == "Date")
     {
         e.Cancel = true;
     
 }

 


Any assistance or guidance would be greatly appreciated

regards steve
0
Richard Slade
Top achievements
Rank 2
answered on 20 Apr 2011, 10:34 AM
Hello,

This is correct behavior. The row index of these system rows (new row etc..) will be -1. Only the data rows will have a row index above -1.

Hope this helps but let me know if you need more information
Richard
0
Stephen Breytenbach
Top achievements
Rank 1
answered on 20 Apr 2011, 10:50 AM

Hi Richard,
thanks for the swift reply, my problem, I gather,  is that it is unbound data, ie I build the Child Grid, Ie, the Data Rows are not linked to a database, would this explain why the RowIndex is -1, even when I click on one of the other (populated) rows? (see attached section of how i populate the child grid)

I need to somehow trigger your solution, but only on the new row, and not the already populated rows (if that makes sense)

Thnx, regards steve

private void radGridView_RowSourceNeeded(object sender, GridViewRowSourceNeededEventArgs e)
  {
              try
              {
                  sps_BillableTableAdapter tblChildAdap = new sps_BillableTableAdapter();
                  String sCharge = e.ParentRow.Cells[1].Value.ToString();
                  Decimal nUnitCost = System.Convert.ToDecimal(e.ParentRow.Cells[3].Value);
                  tblChildAdap.Fill(dts_DWBillDataSet.sps_Billable, sCode, nPeriod, sCharge,nUnitCost);
                  DateTime dDate = new DateTime();
                  this.sDefCharge = e.ParentRow.Cells[1].Value.ToString();  //Set Master Val as default
                  this.sDefDesc = e.ParentRow.Cells[2].Value.ToString(); //Set Master Val as default
                  for (int i = 0; i < dts_DWBillDataSet.sps_Billable.Count; i++)
                  {
                      dts_DWBillDataSet.sps_BillableRow DataRow = dts_DWBillDataSet.sps_Billable[i];
                      GridViewRowInfo row = e.Template.Rows.NewRow();
                      row.Cells["Billable"].Value = DataRow["BillableID"];
                      row.Cells["SO"].Value = DataRow["SO"];
                      row.Cells["ChargeCode"].Value = DataRow["ChargeCode"];
                      row.Cells["Description"].Value = DataRow["Description"];
                      row.Cells["Barcode"].Value = DataRow["Barcode"];
                      row.Cells["Dept"].Value = DataRow["Dept"];
                      if (DataRow["Date"] != null)
                      {
                          dDate = System.Convert.ToDateTime(DataRow["Date"]);
                          row.Cells["Date"].Value = dDate;
                      } row.Cells["Site"].Value = DataRow["Site"];
                      row.Cells["Qty"].Value = DataRow["Qty"];
                      row.Cells["UnitCost"].Value = DataRow["UnitCost"];
                      row.Cells["TotalCost"].Value = DataRow["TotalCost"];
                      row.Cells["Charge"].Value = DataRow["Charge"];
                      row.Cells["Checked"].Value = DataRow["Checked"];
                      e.SourceCollection.Add(row);
                  }
                  tblChildAdap.Dispose();
                  this.InitialLoad = false;
              }
              catch (SqlException ex)
              {
                  MessageBox.Show(ex.Message, "Error Loading Transactions", MessageBoxButtons.OK, MessageBoxIcon.Stop);
              }
  }


0
Julian Benkov
Telerik team
answered on 22 Apr 2011, 02:02 PM
Hi Stephen,

In the Load-On-Demand scenario the RowIndex property is not synchronized with the source collection. For your needs in this scenario you can use ChildRows.IndexOf() method of GridViewTemplate or the parent GridViewRowInfo.

I hope this helps.

Kind regards,

Julian Benkov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Stephen Breytenbach
Top achievements
Rank 1
answered on 22 Apr 2011, 03:12 PM
Hi Julian,

Thanks, just tried, could you please give a sample, as my attempt is still returning -1:

if (this.radGridView.MasterTemplate.ChildRows.IndexOf(e.Row) > -1)

Your assistance is much appreciated

thanks
regards
steve
0
Julian Benkov
Telerik team
answered on 27 Apr 2011, 01:13 PM
Hi Stephen,

Please open a new support ticket and send me a sample application that covers your scenario. This will allow me to review the API used and find the best solution for your case.

Thank you in advance.

Regards,
Julian Benkov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Stephen Breytenbach
Top achievements
Rank 1
answered on 04 May 2011, 01:17 PM
Hi Julian,

I have opened a new ticked, I have attached the Code that I use to Create the Master Child Grids as well as populate them, the Ticket Header is:

RowIndex always -1 using Unbound Grid

Thanks

Regards

Steve
0
Fariba
Top achievements
Rank 1
answered on 25 Jun 2016, 09:36 AM

Hi

I am using hierarchical rad grid view

and I adding Summary row and Adding new row in templates both of them an the bottom position

   grid.Templates[0].AddNewRowPosition = SystemRowPosition.Bottom;

 

       grid.MasterTemplate.Templates[0].SummaryRowsBottom.Add(new GridViewSummaryRowItem(new GridViewSummaryItem[]{
                    new GridViewSummaryItem("customerBankAccount_id", "{0:جمع }", GridAggregateFunction.Sum),
                    new GridViewSummaryItem("amountDetail", "{0}", GridAggregateFunction.Sum)
            }));
            grid.MasterTemplate.ShowTotals = true;
            grid.MasterView.SummaryRows[0].PinPosition = PinnedRowPosition.Bottom;

 

both of them placed in bottom which in what I want.But the problem is the place of adding new row is at the bottom of summary row 

which is not normal and I ewant to place summary row at the bottom ,under the adding row.Please let me to know, How can I solve this issue.

Thx

Best Regards,

Fariba

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 29 Jun 2016, 10:07 AM
Hello Fariba,

Thank you for writing. 

RadGridView allows you to reorder only the system rows: GridFilterRowElement, GridSearchRowElement, GridNewRowElement. The summary rows are placed along with the data rows. If you need to have a summary row at the bottom and the new row to be above it, you can use a simple data row that is pinned to the bottom. However, in this case, it would be necessary to calculate programmatically the summary value by using the ViewCellFormatting event for example and setting the Text property of the cell element.

I hope this information helps. Should you have further questions I would be glad to help.

Regards,
Dess
Telerik
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Tags
GridView
Asked by
Mo
Top achievements
Rank 1
Answers by
Jack
Telerik team
Mo
Top achievements
Rank 1
Ferry
Top achievements
Rank 1
hugo
Top achievements
Rank 2
Julian Benkov
Telerik team
shuyin ngo
Top achievements
Rank 1
Chad Hensley
Top achievements
Rank 1
Rakesh
Top achievements
Rank 1
John Davis
Top achievements
Rank 2
Richard Slade
Top achievements
Rank 2
Stephen Breytenbach
Top achievements
Rank 1
Fariba
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or