Add New Row - Winforms GridView Event

34 posts, 0 answers
  1. Mo
    Mo avatar
    35 posts
    Member since:
    Aug 2006

    Posted 27 Feb 2008 Link to this post

    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
  2. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 27 Feb 2008 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Mo
    Mo avatar
    35 posts
    Member since:
    Aug 2006

    Posted 11 Mar 2008 Link to this post

    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
  5. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 12 Mar 2008 Link to this post

    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
  6. Ferry v.d. Graaf
    Ferry v.d. Graaf avatar
    6 posts
    Member since:
    Sep 2004

    Posted 01 Oct 2008 Link to this post

    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?
  7. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 01 Oct 2008 Link to this post

    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.
  8. hugo
    hugo avatar
    2 posts
    Member since:
    Oct 2007

    Posted 03 Dec 2008 Link to this post

    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
  9. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 04 Dec 2008 Link to this post

    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.
  10. hugo
    hugo avatar
    2 posts
    Member since:
    Oct 2007

    Posted 31 Dec 2008 Link to this post

    it did. thxs a lot.
  11. Ferry v.d. Graaf
    Ferry v.d. Graaf avatar
    6 posts
    Member since:
    Sep 2004

    Posted 20 Nov 2009 Link to this post

    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.
  12. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 25 Nov 2009 Link to this post

    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.
  13. shuyin ngo
    shuyin ngo avatar
    1 posts
    Member since:
    Mar 2010

    Posted 31 Mar 2010 Link to this post

    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? :"(
  14. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 02 Apr 2010 Link to this post

    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.
  15. Chad Hensley
    Chad Hensley avatar
    54 posts
    Member since:
    Feb 2005

    Posted 17 Apr 2010 Link to this post

    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
  16. Rakesh
    Rakesh avatar
    5 posts
    Member since:
    Jul 2010

    Posted 03 Aug 2010 Link to this post

    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. 
  17. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 06 Aug 2010 Link to this post

    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
  18. John Davis
    John Davis avatar
    119 posts
    Member since:
    Apr 2007

    Posted 17 Mar 2011 Link to this post

    how do you set a column as read-only for updating but not read-only for inserting?
  19. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 17 Mar 2011 Link to this post

    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
  20. John Davis
    John Davis avatar
    119 posts
    Member since:
    Apr 2007

    Posted 17 Mar 2011 Link to this post

    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
  21. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 17 Mar 2011 Link to this post

    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
  22. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 17 Mar 2011 Link to this post

    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
  23. John Davis
    John Davis avatar
    119 posts
    Member since:
    Apr 2007

    Posted 17 Mar 2011 Link to this post

    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
  24. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 17 Mar 2011 Link to this post

    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
  25. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 17 Mar 2011 Link to this post

    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
  26. John Davis
    John Davis avatar
    119 posts
    Member since:
    Apr 2007

    Posted 17 Mar 2011 Link to this post

    Richard, As you said, your most recently posted technique works even better.  I found no problems with it.
    John
  27. Stephen Breytenbach
    Stephen Breytenbach avatar
    4 posts
    Member since:
    Aug 2007

    Posted 20 Apr 2011 Link to this post

    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
  28. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 20 Apr 2011 Link to this post

    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
  29. Stephen Breytenbach
    Stephen Breytenbach avatar
    4 posts
    Member since:
    Aug 2007

    Posted 20 Apr 2011 Link to this post

    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);
                  }
      }


  30. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 22 Apr 2011 Link to this post

    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
  31. Stephen Breytenbach
    Stephen Breytenbach avatar
    4 posts
    Member since:
    Aug 2007

    Posted 22 Apr 2011 Link to this post

    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
Back to Top
UI for WinForms is Visual Studio 2017 Ready