Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
Projects.cmdb = new OleDbCommandBuilder(Projects.da);
                    Projects.ds.GetChanges();
                    DataRow rowFound;
                    DataTable tb = Projects.ds.Tables[0];
                    DataColumn[] dc = new DataColumn[1];
                    dc[0] = tb.Columns["UNITSLNO"];
                   
                    tb.PrimaryKey = dc;
                    tb.DefaultView.Sort = "UNITSLNO";
                  
                        if (tb.Rows.Contains(""))
                        {
                       
                            rowFound = tb.Rows.Find("");
                            Projects.ds.Tables[0].Rows.Remove(rowFound);
                            Projects.ds.AcceptChanges();
                        }


the above code as it seems should delete all null rows (any way primary column will not have null value!!!!)if any ,in the datagridview . this i did because if the end user try to add a null value in the primary index key column "UNITSLNO",by mistake ,it should be deleted by executing the above code without further error handling. but unfortunately it do not remove the null rows. i tried with some valid numbers; while executing this code the row got deleted. why it is not happening with null;
Posted
Comments
phil.o 6-Oct-11 6:17am    
Primary keys can NEVER be null.
If you allow such thing, that is your design that should be reviewed ; it's up to you to take care of the primary key, whether by setting it to IDENTITY (it will be autogenerated), whether by setting it by yourself in your backend code. It can't be the responsibility of the user to set the primary key of any record.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900