DataRow which is added is not going to be delete doing table.Rows[i].Delete()? - delete-row

Why is that? The state of the datarow is added. When I delete the row the state is unchanged. Why not deleted? That's the reason my Delete Store Procedure is never called!
edit: the datarow is freshly added and then I try to delete it.

show your code please. my test shows that everything works fine:
DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
Console.WriteLine(dr.RowState);
dr.Delete();
Console.WriteLine(dr.RowState);
output is:
Added
Detached

Related

Unique contstaint error when rejecting changes on a DataTable

I have a datatable problem when I reject changes on the table. I've created an example below which demonstrates the problem:
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Last_Name", typeof(string));
table.Columns.Add("Male", typeof(Boolean));
foreach (DataColumn column in table.Columns)
{
if (column.ColumnName == "Name")
{
column.Unique = true;
}
}
DataRow row;
row = table.NewRow();
row["Name"] = String.Format("Steve");
row["Last_Name"] = String.Format("Smith");
row["Male"] = true;
table.Rows.Add(row);
table.AcceptChanges();
So at this stage I have a DataTable with a unique column constraint and 1 row in that table.
I now delete that row:
row.Delete();
This sets the rowstate to Deleted.
At this stage I realise that I've made a mistake and want to add the row again. So I create the new row again and add it to the DataTable:
row = table.NewRow();
row["Name"] = String.Format("Steve");
row["Last_Name"] = String.Format("Smith");
row["Male"] = true;
table.Rows.Add(row);
At this stage my DataTable contents are in the following state:
table.Rows[0].RowState is Deleted
table.Rows[1].RowState is Added
Now, I've changed my mind about the whole thing and want to get back to how I started so I call RejectChanges:
table.RejectChanges();
When I do this I receive the following error:
Column 'Name' is constrained to be unique. Value 'Steve' is already present.
I understand that there are 2 rows with the same values but I thought reject changes would have ingored this as the RowStates are different.
Any ideas how I can get round this?
In my live code I use this to move rows between to 2 grids (like allowing the user to selected what columns are visible)
I'm using C#4.0 in Visual Studio 2012
Thanks in advance

deleting row from datagridview

I want to delete row from datagridview when user clicks the delete button.The datagridview bounded to datatable _dt so i remove the row from _dt and try to rebind the modified _dt to datagridview but instead of deleting the row gets added to the last row of the datagridview what am I doing wrong here
private void btnDelete_Click(object sender, EventArgs e)
{
if (dataGridView1 != null && dataGridView1.SelectedRows.Count == 1)
{
string key = dataGridView1.SelectedRows[0].Cells["ID"].Value.ToString();
DataColumn[] keyColumns = new DataColumn[1];
keyColumns[0] = _dt.Columns["ID"];
_dt.PrimaryKey = keyColumns;
rowToDelete = _dt.Rows.Find(key);
_dt.Rows.Remove(rowToDelete);
_dt.AcceptChanges();
SqlCommandBuilder cb = new SqlCommandBuilder(_sqlDa);
_sqlDa.Fill(_dt);
_sqlDa.Update(_dt);
dataGridView1.DataSource = null;
dataGridView1.DataSource = _dt;
}
}
No need for Fill before update and also delete this row
_dt.AcceptChanges();
because this will remove the row from memory and when you call update
only the remaining rows are updated, it will not delete the row from database.
No need to call Fill Before Update, because Fill will get data again from db and your changes will be lost. So This will work for you.
SqlCommandBuilder cb = new SqlCommandBuilder(_sqlDa);
//_sqlDa.Fill(_dt);
_sqlDa.Update(_dt);

ColumnCount in DataGridView remains 0 after assigning data source

I am having trouble with the following simple code
BindingList<Car> tempList = new BindingList<Car>();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = tempList;
dgTempView.DataSource = bindingSource;
Here, dgTempView is a data grid view
After the above lines execute, the column count in the datagrid view remains 0. And when I try adding a Car instance in tempList, I get an error saying that 'no row can be added to a datagridview control that does not have columns' . I am not able to understand what am I missing here
Found my mistake. The members of the Class Car were public instance variables and not properties. The moment I changed them to Auto Properties it worked :)

Setting DisplayMemberPath of ComboBox in code

In my WPF program I have:
string queryString = "Select AccountID, ProjectName from Foo where IsEnabled = 1";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, sConn1);
DataSet dsAccounts = new DataSet();
adapter.Fill(dsAccounts, "Accounts");
cbAccount.ItemsSource = dsAccounts.Tables["Accounts"].AsEnumerable();
cbAccount.DisplayMemberPath = "ProjectName";
When my program runs and I dropdown the ComboBox all the rows are there but they display as blanks. When I click on a row, my SelectionChanged event handler properly identifies the selected row and picks up the proper values.
I believe my problem is with the DisplayMemberPath.
What am I doing wrong?
This is not an answer but rather a workaround. This works:
cbAccount.DataContext = dsAccounts.Tables["Accounts"];
//cbAccount.ItemsSource = dsAccounts.Tables["Accounts"].AsEnumerable();
cbAccount.DisplayMemberPath = "ProjectName";
By setting the DataContext reather than the ItemSource then the DisplayMemberPath is being set properly.
The question remains open, there must be a way to properly set the DisplayMemberPath when one has an ItemSource rather than a DataContext.
I believe the problem is that your table accounts is not serialized to objects.
If you use a list of accounts instead of your tables then it works perfect with the ItemsSource and DisplayMemeberPath.

select the last row in DataGridView when I load the form using c#

I need to select the last row in DataGridView, when form loads the first time. So
I wrote this code in Form_Load event :
da.SelectCommand = new SqlCommand("SELECT * FROM Days", cn);
ds.Clear();
da.Fill(ds);
dataGridView1.DataSource = dsD.Tables[0];
dataGridView1.ClearSelection();
int nRowIndex = dataGridView1.Rows.Count - 1;
dataGridView1.Rows[nRowIndex].Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = nRowIndex;
dataGridView1.Refresh();
But this isnĀ“t doing anything. It still selects the first row, but this code works when I save the data in the database and then fill the dataset. But why not the first time when I load the form.
I used :
dataGridView1.CurrentCell = dataGridView1.Rows[nRowIndex].Cells[0];
and it worked.

Resources