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

Add New Row Issue

4 Answers 54 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Exchange
Top achievements
Rank 1
Exchange asked on 04 Nov 2014, 04:49 PM
So I have a RadGridView  that is bound to a "BindingSource." The bindingsource is populated using a SqlDataAdapter.
The data being pulled in has an auto-incrementing Identity Column (ID) as it's primary key. 

I have successfully bound the grid, I can edit records and successfully see the changes in the database. However when I add a record, the data is successfully added to the database, but in the grid the "ID" field shows a value of "0" instead of the actual new value. How do i refresh the grid to show the new information? 

Thank you!

4 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 07 Nov 2014, 11:33 AM
Hello ,

Thank you for writing.

When you create a DataSet from the MS SQL server database, several properties of the auto number fields are wrongly set. Please refer to the attached screenshot illustrating what should be changed in order to obtain the correct auto-incremented id in the RadGridView new row.

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

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Exchange
Top achievements
Rank 1
answered on 07 Nov 2014, 02:59 PM
previously I had this in the binding code:
if (isAutoField)
{
    ds.Tables[0].Columns[PrimaryKey].AutoIncrement = true;
    ds.Tables[0].Columns[PrimaryKey].AutoIncrementStep = 1;
}

I have added 
        "ds.Tables[0].Columns[PrimaryKey].AutoIncrementSeed = 1;"

However now the error i receive is:
     "Column is constrained to be unique. Value '1' is already present. 

Suggestions?

0
Exchange
Top achievements
Rank 1
answered on 07 Nov 2014, 03:15 PM
Nevermind, I set the seed to the max id value +1 and it was able to save successfully. Thank you! 
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 12 Nov 2014, 12:30 PM
Hello,

Thank you for writing back.

I am glad that you have found a solution which suits your requirement. However, if you prefer to modify the described changes at run time, you should set the AutoIncrement, AutoIncrementStep and AutoIncrementSeed properties before filling the table adapter:
private void Form1_Load(object sender, EventArgs e)
{
    this.nwindDataSet.Tables[0].Columns[0].AutoIncrement = true;
    this.nwindDataSet.Tables[0].Columns[0].AutoIncrementStep = 1;
    this.nwindDataSet.Tables[0].Columns[0].AutoIncrementSeed = 1;
 
    this.categoriesTableAdapter.Fill(this.nwindDataSet.Categories);
    this.radGridView1.DataSource = this.categoriesBindingSource;
}

Thus, when adding a new row, the "ID" column will be auto-incremented.

I hope this information helps. If you have any additional questions, please let me know.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Exchange
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Exchange
Top achievements
Rank 1
Share this question
or