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

Problems with CurrentRowChanging event

1 Answer 321 Views
GridView
This is a migrated thread and some comments may be shown as answers.
John Hughes
Top achievements
Rank 1
John Hughes asked on 29 Apr 2010, 08:27 PM
I am trying to use the CurrentRowChanging event to determine when the user changes a row in the grid.  My grid is hierarchical with tabbed child views.  I have a column that is hidden on the parent row for "comments".  Instead, I have a text entry box else where in the UI that the user can type comments into for that row (parent row only).  This is working fine when I only select parent rows.  When I have expanded the child view and select a row there and then select a parent row, my code crashes.  I presume  that's because these are different tables in my data set.   Is there an easy way from the event data to determine if I have two different tables/objects?

Code snippet here:


        // This method will catch the CurrentRowChanging event in the Grid and will
        // handle updating changing the comments in the text widget for each Job.
        private void radGridView1_CurrentRowChanging(object sender, CurrentRowChangingEventArgs e)
        {
            // Check for error conditions
            if ((e.CurrentRow == null) || (e.NewRow == null))
            {
                return;
            }

            if (e.NewRow.Cells["JobName"].Value == null)
                return;

            string newJobName = (string)e.NewRow.Cells["JobName"].Value;
            string oldJobName = (string)e.CurrentRow.Cells["JobName"].Value;

            // They changed rows within the job, but did not change jobs
            if (newJobName == oldJobName)
            {
                return;
            }

            // save current text into old job
            if (radTextBox1.Text == "")
            {
                e.CurrentRow.Cells["Comments"].Value = "";
            }
            else
            {
                e.CurrentRow.Cells["Comments"].Value = radTextBox1.Text;
            }

            // put new job comments into text widget
            if (e.NewRow.Cells["Comments"].Value.ToString() == "")
            {
                radTextBox1.Text = "";
            }
            else
            {
                radTextBox1.Text = (string)e.NewRow.Cells["Comments"].Value;     <<<< ------  crashes here
            }
        }


It crashes I'm sure because Comments is not a valid column for the child table.

Any suggestions would be appreciated.

John

1 Answer, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 30 Apr 2010, 05:45 PM
Hi John Hughes,

You can distinguish the row by determining which is its data bound item. This is the record that comes from the data source of the grid. If the data source is DataTable (or DataSet) the data bound item is DataRowView. If the data source is a custom collection, the data bound item is the object from your custom type. Hence, you can understand the type of the object and determine the type of the row. If you are using DataTable as your data source you can determine which is the table.

You can use the following code snippet:

private void radGridView_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
{
    if (e.CurrentRow == null || e.OldRow == null)
    {
        return;
    }
 
    DataRowView rowView = e.CurrentRow.DataBoundItem as DataRowView;
    DataTable rowTable = rowView.Row.Table;
 
    if (rowTable == productTable)
    {
 
    }
}

If you are using custom objects you can use this code snippet:

private void radGridView_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
{
    if (e.CurrentRow == null || e.OldRow == null)
    {
        return;
    }
 
    if (e.CurrentRow.DataBoundItem is Product)
    {
 
    }
}

Sincerely yours,
Svett
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.
Tags
GridView
Asked by
John Hughes
Top achievements
Rank 1
Answers by
Svett
Telerik team
Share this question
or