Click here to Skip to main content
15,915,032 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a datatable, when i remove row from it, it will throws an error that table does not have primary key.
any one have solution for it?

i want to remove selected item of combobox from the datatable.

What I have tried:

private void dropDownDemo_Load(object sender, EventArgs e)
       {

           dt.Columns.Add("Data");
           dt.Rows.Add("None");
           dt.Rows.Add("1st");
           dt.Rows.Add("2nd");
           dt.Rows.Add("3rd");
       }


public void removeSelected(string selected)
        {
            if (selected != "None")
            {
                foreach (DataRow row in dt.Rows)
                {
                    if (dt.Rows.Contains(selected))    //throw error here :- System.Data.MissingPrimaryKeyException: Table doesn't have a primary key.
                    {
                        dt.Rows.Remove(row);
                    }
                }
            }
        }
Posted
Updated 11-Jul-17 5:47am
v2

Hi,

Below, code to delete rows.

C#
public void removeSelected(string selected)
        {
            try
            {
                if (selected != "None")
                {
                    DataRow[] drs = dt.Select("Data = '" + selected + "'");

                    if (drs.Length > 0)
                    {
                        foreach (DataRow dr in drs)
                        {
                            dr.Delete();
                            dt.AcceptChanges();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message; 
            }
        }
 
Share this answer
 
v2
Replacing your foreach loop with the below loop to identify the specific row and then delete it should do the trick.

for(int i = dt.Rows.Count-1; i >= 0; i--)
{
DataRow dr = dt.Rows[i];
if (dr["Data"] == ((ComboBox)sender).Text)
dr.Delete();
}
 
Share this answer
 
The rows are identified and compared via a primary key. You're asking if the set of rows contains a row with the primary key selected.

Also, you should never make changes to an iterator within the iteration. This will throw an error also.

Try this instead:

C#
private void dropDownDemo_Load(object sender, EventArgs e)
        {
 
            dt.Columns.Add("Data");
            dt.Rows.Add("None");
            dt.Rows.Add("1st");
            dt.Rows.Add("2nd");
            dt.Rows.Add("3rd");
        }
public void removeSelected(string selected)
        {
            var dtTemp = new DataTable;
            dtTemp .Columns.Add("Data");
            if (selected != "None")
            {
                foreach (DataRow row in dt.Rows)
                {
                    if (!dt.Rows["Data"].Contains(selected))   
                    {
                        dtTemp.Rows.Add(row)
                    }
                }
            }
            dt = dtTemp;
        }


Another way would be to add a Break; after you remove the row, but this is pretty clean
 
Share this answer
 
None of the other solutions seem to have picked up on the fact you have not added a primary key to your datatable...Contains will not work without a primary key (Reference DataColumnCollection.Contains Method (String) (System.Data)[^] )
private void dropDownDemo_Load(object sender, EventArgs e)
{
 
            dt.Columns.Add("Data");
            dt.Rows.Add("None");
            dt.Rows.Add("1st");
            dt.Rows.Add("2nd");
            dt.Rows.Add("3rd");

            dt.PrimaryKey = new DataColumn[] { dt.Columns["Data"] };
}


You can then use Contains (NB untested)

C#
public void removeSelected(string selected)
{
    if (selected != "None")
    {
        while (dt.Rows.Contains(selected))
        {
            DataRow foundRow = table.Rows.Find(selected);
            if(foundRow != null)
            {
                dt.Rows.Remove(foundrow);
            }
        }
    }
}
 
Share this answer
 

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