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

Gridview Insert problem

7 Answers 494 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sandi Markon
Top achievements
Rank 1
Sandi Markon asked on 07 Dec 2009, 10:03 AM
I wish to have a form with a gridview on one side and a panel with current record data on the other side (See attached file) so that I can edit data in the gridview or on the panel. Editing works fine, but problems appear on inserting.

Code behind update button is following:

        private void UpdateGridInfo(GridViewRowInfo currentRow)
        {
            if (currentRow == null)
                return;
            DB.Contract rowView = (DB.Contract)currentRow.DataBoundItem;
            if (rowView.ID == 0)
            {
                // insert
                DB.Contract contract = new DB.Contract();
                contract.RadioStationID = ((DB.vRadioStation)bsRadioStations2.Current).acSubject;
                ...
                App.ContractsDAL.DC.Contracts.InsertOnSubmit(contract);
            }
            else
            {
                // edit
                rowView.RadioStationID = ((DB.vRadioStation)bsRadioStations2.Current).acSubject;
                ...
            }
        }

        private void bUpdate_Click(object sender, EventArgs e)
        {
                UpdateGridInfo(gvContracts.CurrentRow);
                App.ContractsDAL.SubmitChanges();
                App.ContractsDAL.RefreshDC();
                bsContracts.DataSource = App.ContractsDAL.Get();
        }

When I click add a new row on the gridview and then edit data on the panel, I get exceptions for NOT NULL columns, foreign keys... when I try to submit changes, for example:
Cannot insert the value NULL into column 'RadioStationID', table 'RadioStations.dbo.Contracts'; column does not allow nulls. INSERT fails.
The statement has been terminated.

If I debug program, I see that contract.RadioStationID is set to correct value. At first it seems, that gridview automatically posts NULL values, but if I set columns to Nullable and delete relationships from database, everything works as it is supposed to.

How could I make this work without setting columns to Nullable and removing relationships?

7 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 09 Dec 2009, 12:15 PM
Hello Sandi Markon,

When you create a new row, the default value for RadioStationID is null. You can set a value for RadioStationID if you make the column type of this field (TextBoxColumn or ComboBoxColumn), or another solution is to use DefaultValueNeeded event:

void radGridView1_DefaultValuesNeeded(object sender, GridViewRowEventArgs e)
{
    DB.Contract rowView = (DB.Contract)e.Row.DataBoundItem;
    rowView..RadioStationID = ... //assign default value
}

Regards,
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
Sandi Markon
Top achievements
Rank 1
answered on 15 Jan 2010, 11:03 AM
Given solution works if I set default values to valid data. What if I want to have empty data for default values? I don't want to set data with some values, because some users will forget to change them to correct values. In given solution everytime two inserts happens - one with my data and one with default values, which are deleted at the end, but if I want to have empty default values, this insert fails.
0
Julian Benkov
Telerik team
answered on 20 Jan 2010, 08:39 AM
Hi Sandi Markon,

This behavior depends on your data validation logic -- in your case constraints applied to the DataTable objects. If you have RadioStationID constraint for the null value you cannot add a new row with the RadioStationID being null. Please review your database schema or typed dataset in your Visual Studio. You can find more information here.

Kind regards, 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
Sandi Markon
Top achievements
Rank 1
answered on 21 Jan 2010, 07:44 AM
It is correct, that in database RadioStationID is not null and that default values are empty. The problem is that when I submit data where RadioStationID is not null, before the correct row is inserted, the row with default values is inserted and deleted and I do not know why.
0
Julian Benkov
Telerik team
answered on 25 Jan 2010, 11:01 AM
Hello Sandi Markon,

The current behavior for inserting a row in RadGridView is just like in the standard DataGridView. When you click to add new row in a cell of the new row, the row is added to the underlying datasource and the data bound item is populated with values from DefaultValuesNeeded. In this situation, if you Escape(Cancel) the new row, it will be removed from the underlying DataSouce. We plan to change this behavior in one of our next releases and a row will be added after committing a new row.

If you continue experiencing the issue, please send me a small project with data to debug it locally.

All the best,
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
Sandi Markon
Top achievements
Rank 1
answered on 16 Jul 2010, 01:30 PM
It seems that with the new build you have changed behavior of DefaultValuesNeeded function. I have updated my project and now e.Row.DataBoundItem in this function is always null.
0
Accepted
Jack
Telerik team
answered on 20 Jul 2010, 06:46 AM
Hello Sandi Markon,

Thank you for contacting us. Yes, we changed the behavior when adding new rows in RadGridView. Now, the actual new row is created when the validation process is finished. When handling DefaultValuesNeeded event there is no DataBoundItem. You should use e.Row.Cells collection directly. 

In case you still want to access the DataBoundItem property, you should do this when handling UserAddedRow event:

void radGridView1_UserAddedRow(object sender, GridViewRowEventArgs e)
{
    object dbi = e.Row.DataBoundItem;
    //...
}

Please tell me whether this helps. If you need further assistance, I will be glad to help.

 

Best wishes,
Jack
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
Sandi Markon
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Sandi Markon
Top achievements
Rank 1
Jack
Telerik team
Share this question
or