change text in the cell

12 posts, 1 answers
  1. Sam
    Sam avatar
    14 posts
    Member since:
    Sep 2012

    Posted 11 Oct 2012 Link to this post

    When I'm changing text in textbox - the value in the cell is changing too(at the same time). I use event
    private void rtx_TextChanging(object sender, TextChangingEventArgs e)
    {
        RadTextBox rtb = sender as RadTextBox;
        string title = rtb.Name.Substring(0, rtb.Name.Length - "textbox".Length);
        this.radLabelElement1.Text = rgv.CurrentRow.Cells[title].Value.GetType().ToString();
        rgv.CurrentRow.Cells[title].Value = e.NewValue;
        rgv.CurrentRow.InvalidateRow();
    }
    where rtx - radtextbox

    What event? I need to use to do the same thing for textboxes - when I change value in the cell the value in textbox changes too. I was trying to use CellValueChanged but it affects only after I press enter
  2. Answer
    Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 16 Oct 2012 Link to this post

    Hello Jei,

    Thank you for writing.

    To capture the text changed in a cell in the grid, you need to subscribe to the TextChanged event of the cell editor. Here is how to do that for RadTextBoxEditor:
    Private Sub RadGridView1_CellEditorInitialized(sender As Object, e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles RadGridView1.CellEditorInitialized
           Dim textBoxEditor As RadTextBoxEditor = TryCast(e.ActiveEditor, RadTextBoxEditor)
     
           If textBoxEditor IsNot Nothing Then
               Dim textBoxEditorElement As RadTextBoxEditorElement = DirectCast(textBoxEditor.EditorElement, RadTextBoxEditorElement)
               RemoveHandler textBoxEditorElement.TextChanged, AddressOf editorTextChanged
               AddHandler textBoxEditorElement.TextChanged, AddressOf editorTextChanged
           End If
       End Sub
     
       Private Sub editorTextChanged(sender As Object, e As EventArgs)
           Dim textBoxEditorElement As RadTextBoxEditorElement = DirectCast(sender, RadTextBoxEditorElement)
           radLabel1.Text = textBoxEditorElement.Text
       End Sub

    Note that it is important to unsubscribe before subscribing, because editors in the grid are reused and this way you will avoid multiple subscriptions.

    I hope this helps.
     
    Regards,
    Stefan
    the Telerik team
    You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Sam
    Sam avatar
    14 posts
    Member since:
    Sep 2012

    Posted 21 Oct 2012 Link to this post

    Thanks!
  5. Sam
    Sam avatar
    14 posts
    Member since:
    Sep 2012

    Posted 21 Oct 2012 Link to this post

    so It should work?(I can't check now because I've got problems with datagridview)
    void rgv_CellEditorInitialized(object sender, GridViewCellEventArgs e)
            {
                RadTextBoxEditor rtbe = e.ActiveEditor as RadTextBoxEditor;
                index_column = e.ColumnIndex;
                if (rtbe != null)
                {
                    RadTextBoxEditorElement rtbee = rtbe.EditorElement as RadTextBoxEditorElement;
                    rtbee.TextChanged -= rtbee_TextChanged;
                    rtbee.TextChanged += rtbee_TextChanged;
                }
            }
     
            void rtbee_TextChanged(object sender, EventArgs e)
            {
                RadTextBoxEditorElement rtbee = sender as RadTextBoxEditorElement;
                string header = rgv.Columns[index_column].HeaderText;
                this.Controls[header + "textbox"].Text = rtbee.Text; // I give names to textboxes like this - headertext+textbox
            }
  6. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 23 Oct 2012 Link to this post

    Hi Jei,

    In my tests the code that I provided works just fine. In yours you seems to be using the column index, which if it changes might lead to undesired results.  I would suggest that you use the column Name instead.

    I hope this helps.

    Kind regards,
    Stefan
    the Telerik team
    You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.
  7. Sam
    Sam avatar
    14 posts
    Member since:
    Sep 2012

    Posted 23 Oct 2012 Link to this post

    I don't know either new problem comes from http://www.telerik.com/community/forums/winforms/gridview/change-color-of-the-cell-when-its-value-was-changed.aspx or this thread(or maybe both) but I've got messagebox Data Exception 'idclient is read only'.

    How to handle this exception? I put try - catch in every method but still I have this messagebox.

    Also there is several problems. For example there is a datagrid with columns id, name,middlename,surname,address,telephone,props,contacts and there is also several textboxes.

    When I click on idclient cell I've got messagebox - DataException "idclient is read only". It changes its color to red and cell on the same row in the column [props] changes color too. 
    When I click on cell(edit mode activated) and the click on other cell - the value from new cell copies to previous cell.
    When I click on the cell in another row - I've got data exception  and [idclient] and [props] cells change its color too.

    void rgv_CurrentCellChanged(object sender, CurrentCellChangedEventArgs e)
     {
         try
         {
             if (e.CurrentCell.RowInfo.Index == e.NewCell.RowInfo.Index)
                 return;
             else
             {
                 for (int i = 0; i < rgv.Columns.Count; i++) //copy all data from datagrid cells of the current row to textboxes
                 {
                     string val = rgv.Rows[e.NewCell.RowInfo.Index].Cells[i].Value.ToString();
                     this.Controls[rgv.Columns[i].HeaderText + "textbox"].Text = val;
                 }
             }
         }
         catch (NullReferenceException)
         {
         }
         catch (DataException) { return; }
     }
     
     void rgv_CellValueChanged(object sender, GridViewCellEventArgs e)
     {
         try
         {
             if (e.Row.Tag == null)
             {
                 e.Row.Tag = e.Column.Name;
             }
     
             if (!e.Row.Tag.ToString().Contains(e.Column.Name))
             {
                 e.Row.Tag += e.Column.Name;
             }
         }
         catch (DataException) { };
     }
     
     void rgv_CellFormatting(object sender, CellFormattingEventArgs e)
     {
         try
         {
             if (e.Row.Tag != null && e.Row.Tag.ToString().Contains(e.Column.Name))
             {
                 e.CellElement.DrawFill = true;
                 e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
                 e.CellElement.BackColor = Color.Red;
             }
             else
             {
     
                 e.CellElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
                 e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local);
                 e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, Telerik.WinControls.ValueResetFlags.Local);
             }
         }
         catch (DataException) { }
     }
     
     void rgv_CellEditorInitialized(object sender, GridViewCellEventArgs e)
     {
         try
         {
             RadTextBoxEditor rtbe = e.ActiveEditor as RadTextBoxEditor;
             name_column = e.Column.Name;
             if (rtbe != null)
             {
                 RadTextBoxEditorElement rtbee = rtbe.EditorElement as RadTextBoxEditorElement;
                 rtbee.TextChanged -= rtbee_TextChanged;
                 rtbee.TextChanged += rtbee_TextChanged;
             }
         }
         catch (DataException) { }
     }
     
     void rtbee_TextChanged(object sender, EventArgs e)
     {
         try
         {
             RadTextBoxEditorElement rtbee = sender as RadTextBoxEditorElement;
             string header = rgv.Columns[name_column].HeaderText;
             this.Controls[header + "textbox"].Text = rtbee.Text;
         }
         catch (DataException) { }
     }
     
     
    private void rtx_TextChanging(object sender, TextChangingEventArgs e)
     {
         try
         {
             RadTextBox rtb = sender as RadTextBox;
             string title = rtb.Name.Substring(0, rtb.Name.Length - "textbox".Length);
             this.radLabelElement1.Text = rgv.CurrentRow.Cells[title].Value.GetType().ToString();
             try
             {
                 rgv.CurrentRow.Cells[title].Value = e.NewValue; //maybe this causes exception?
             }
             catch (FormatException) { e.Cancel = true; }
             rgv.CurrentRow.InvalidateRow();
         }
         catch (DataException) { };
     }
  8. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 26 Oct 2012 Link to this post

    Hi Sam,

    Thank you for writing.

    I was not able to replicate the experienced exception on my end, neither I was able to replicate the copy value from one cell to another.

    Attached you can find a project with your code and some improvements regarding the coloring issue - the logic that I provided was just a basic example and in your case it breaks because when you click the surname column, the "surname" string gets into the row tag and then in the cell formatting, when we check for surname the result is true and the cell is colored, but when we check if the string contains "name" it will also be true (since surname contains name) and the cell in the name column will be colored too. I have replaced the string with a list of strings which should handle this case.

    Please provide me with exact steps that I need to follow in order to reproduce the copy issue and the exception in the attached application, so I can investigate it and provide you with adequate support.

    All the best,
    Stefan
    the Telerik team
    Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.
  9. Sam
    Sam avatar
    14 posts
    Member since:
    Sep 2012

    Posted 27 Oct 2012 Link to this post

    The problem "column is read-only" was the problem with dataset because idclient is a primary key. I've set readonly property as false for dataset and simply set property readonly for all cells in idclient colulmn.
    The problem with column [props] coloring is still remains but whatever....

    And new problem appeared - when I edit cell and then press enter - text from textbox dissappeared in your project(editcell.png->enterpressed.png). The same problem when I edit one cell and then click on another cell(editcell2.png->changedcell.png)
  10. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 31 Oct 2012 Link to this post

    Hi,

    I am glad to hear that you have resolved the "columns read only issue".

    In regards to the [props] coloring issue, please provide me with a step by step guide how to reproduce it in my project, so I can help you with it.

    About the missing text, you should update the text boxes on CellEndEdit or CellValueChanged event:
    void rgv_CellEndEdit(object sender, GridViewCellEventArgs e)
    {
        foreach (GridViewCellInfo cell in e.Row.Cells)
        {
            this.Controls[cell.ColumnInfo.HeaderText + "textbox"].Text = cell.Value.ToString();
        }
    }

    Attached is the modified version of the project.
     
    Greetings,
    Stefan
    the Telerik team
    Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.
  11. Sam
    Sam avatar
    14 posts
    Member since:
    Sep 2012

    Posted 03 Nov 2012 Link to this post

    I solved all the problems.It doesn't matter now because trial version has already expired but I decided to share my solution.
    I've created KeyDown event for textbox
    rtx.KeyDown += rtx_KeyDown;

    Inside KeyDown event I add TextChanged event
    void rtx_KeyDown(object sender, KeyEventArgs e)
            {
                RadTextBox t = sender as RadTextBox;
                t.TextChanged +=t_TextChanged;
            }

    Here is the code for TextChanged event.
    void t_TextChanged(object sender, EventArgs e)
           {
               int index = rgv.CurrentRow.Index;
               RadTextBox rtb = sender as RadTextBox;
               string textboxname = (sender as RadTextBox).Name;
               string sub = textboxname.Substring(0, textboxname.Length - "textbox".Length);
               int index1 = rgv.Columns[sub].Index;
               if (rgv.CurrentRow.Cells[index1].RowInfo.Tag == null)
               {
                   List<string> list = new List<string>();
                   list.Add(rgv.CurrentRow.Cells[index1].ColumnInfo.Name);
                   rgv.CurrentRow.Cells[index1].RowInfo.Tag = list;
               }
               else
               {
                   List<string> list = rgv.CurrentRow.Cells[index1].RowInfo.Tag as List<string>;
                   list.Add(rgv.CurrentRow.Cells[index1].ColumnInfo.Name);
                   rgv.CurrentRow.Cells[index1].RowInfo.Tag = list;
               }
               try
               {
                   try
                   {
                       rgv.Rows[index].Cells[index1].Value = rtb.Text;
                   }
                   catch (ArgumentOutOfRangeException) { this.radLabelElement1.Text = "Too big elem"; }
               }
               catch (FormatException) { this.radLabelElement1.Text = "Incorrect format"; }
               finally
               {
                   RadTextBox t = sender as RadTextBox;
                   t.TextChanged -= t_TextChanged;//it's important!
               }
           }

    And there are other events. But I left it without any serious changes.
    void rgv_CellValueChanged(object sender, GridViewCellEventArgs e)
            {
                try
                {
                    this.Controls[e.Column.Name + "textbox"].Text = e.Value.ToString();
                }
                catch (NullReferenceException) { }
            }
             
            void rgv_CurrentCellChanged(object sender, CurrentCellChangedEventArgs e)
            {
                try
                {
                    if (e.CurrentCell.RowInfo.Index == e.NewCell.RowInfo.Index)
                        return;
                    else
                    {
                        //iterate through textboxes
                        for (int i = 0; i < rgv.Columns.Count; i++)
                            this.Controls[rgv.Columns[i].HeaderText + "textbox"].Text = rgv.CurrentRow.Cells[i].Value.ToString();
                    }
                }
                catch (NullReferenceException) { };
            }
     
            void rgv_CellEndEdit(object sender, GridViewCellEventArgs e)
            {
                if (e.Row.Tag == null)
                {
                    List<string> list = new List<string>();
                    list.Add(e.Column.Name);
                    e.Row.Tag = list;
                }
                else { List<string> list = e.Row.Tag as List<string>; list.Add(e.Column.Name); e.Row.Tag = list; }
            }
     
            void rgv_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
            {
                if (e.Column.HeaderText.Substring(0, 2).Equals("id")) e.Cancel = true;
                RadTextBoxEditor rtbee = e.ActiveEditor as RadTextBoxEditor;
                name_column = e.Column.Name;
                if (rtbee != null)
                {
                    rtbee.ValueChanging += rtbee_ValueChanging;
                }
            }
     
            void rtbee_ValueChanging(object sender, ValueChangingEventArgs e)
            {
                this.Controls[name_column + "textbox"].Text = e.NewValue.ToString();
            }
     
            void rgv_CellFormatting(object sender, CellFormattingEventArgs e)
            {
                if (e.Row.Tag != null && e.Row.Tag is List<string> && ((List<string>)e.Row.Tag).Contains(e.Column.Name))
                {
                    e.CellElement.DrawFill = true;
                    e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
                    e.CellElement.BackColor = Color.Red;
                }
                else
                {
                    e.CellElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
                    e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local);
                    e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, Telerik.WinControls.ValueResetFlags.Local);
                }
            }
    It work correct however it is not the best solution I think
  12. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 07 Nov 2012 Link to this post

    Hello Jei,

    I am glad that everything is working fine on your end. Thank you for sharing your code with the community.

    Kind regards,
    Stefan
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
  13. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 07 Nov 2012 Link to this post

    Hello Sam,

    One quick remark here, the CellValueChanged event is fired just when the cells value is actually changed (typically on loosing focus), but the ValueChanged event is fired when you type something in the editor, for eg in a textbox editor it is fired for every keystroke.

    You could use that one and have the required checks there.

    If you have any other questions, please let me know.

    Best Regards,
    Emanuel Varga
    Winforms MVP
Back to Top
UI for WinForms is Visual Studio 2017 Ready