DataTable / DataRow / someDataTable.Select() Issue
-
Hey Guys i need to filter the data in my DataGridView, when the window opens i query my db and bind the data to a data table and the to the data grid... this works just fine.. now the user must be able so filter these rows, here is my current code...
private void btnSearch_Click(object sender, EventArgs e) { DataTable filterTable = new DataTable(); string filterString = null; string action = cmbAction.SelectedItem.ToString(); switch(action) { case"All": filterString = ""; break; case"Add": filterString = "Action='add'"; break; case"Edit": filterString = "Action='edt'"; break; case"Delete": filterString = "Action='del'"; break; } DataRow[] filteredRowsArray = table.Select(filterString); MessageBox.Show("Rows = " + filteredRowsArray.Length); if (filteredRowsArray.Length == 0) { //moan } else { filterTable.Clear(); foreach (DataRow newRow in filteredRowsArray) { filterTable.ImportRow(newRow); //this seems to be the problem... } dataGridView.DataSource = filterTable; } }
everything is fine up untill i callfilterTable.ImportRow(newRow)
, i think its adding BLANK rows... if i foreach filterTable.Rows the rows are empty :confused: i also triedfilterTable.Rows.Add(newRow);
but then it moans "This row already belongs to another table." anybody hav some ideas? ThanxHarvey Saayman - South Africa Junior Developer .Net, C#, SQL think BIG and kick ASS
you.suck = (you.passion != Programming)
-
Hey Guys i need to filter the data in my DataGridView, when the window opens i query my db and bind the data to a data table and the to the data grid... this works just fine.. now the user must be able so filter these rows, here is my current code...
private void btnSearch_Click(object sender, EventArgs e) { DataTable filterTable = new DataTable(); string filterString = null; string action = cmbAction.SelectedItem.ToString(); switch(action) { case"All": filterString = ""; break; case"Add": filterString = "Action='add'"; break; case"Edit": filterString = "Action='edt'"; break; case"Delete": filterString = "Action='del'"; break; } DataRow[] filteredRowsArray = table.Select(filterString); MessageBox.Show("Rows = " + filteredRowsArray.Length); if (filteredRowsArray.Length == 0) { //moan } else { filterTable.Clear(); foreach (DataRow newRow in filteredRowsArray) { filterTable.ImportRow(newRow); //this seems to be the problem... } dataGridView.DataSource = filterTable; } }
everything is fine up untill i callfilterTable.ImportRow(newRow)
, i think its adding BLANK rows... if i foreach filterTable.Rows the rows are empty :confused: i also triedfilterTable.Rows.Add(newRow);
but then it moans "This row already belongs to another table." anybody hav some ideas? ThanxHarvey Saayman - South Africa Junior Developer .Net, C#, SQL think BIG and kick ASS
you.suck = (you.passion != Programming)
hi... what your thinking is rite..... If the DataRow that is passed as a parameter is in a detached state, it is ignored, and no exception is thrown while importing the datarow...
Regards, Sandeep Kumar.V
-
hi... what your thinking is rite..... If the DataRow that is passed as a parameter is in a detached state, it is ignored, and no exception is thrown while importing the datarow...
Regards, Sandeep Kumar.V
okay... then how do i get my desired result?
Harvey Saayman - South Africa Junior Developer .Net, C#, SQL think BIG and kick ASS
you.suck = (you.passion != Programming)
-
Hey Guys i need to filter the data in my DataGridView, when the window opens i query my db and bind the data to a data table and the to the data grid... this works just fine.. now the user must be able so filter these rows, here is my current code...
private void btnSearch_Click(object sender, EventArgs e) { DataTable filterTable = new DataTable(); string filterString = null; string action = cmbAction.SelectedItem.ToString(); switch(action) { case"All": filterString = ""; break; case"Add": filterString = "Action='add'"; break; case"Edit": filterString = "Action='edt'"; break; case"Delete": filterString = "Action='del'"; break; } DataRow[] filteredRowsArray = table.Select(filterString); MessageBox.Show("Rows = " + filteredRowsArray.Length); if (filteredRowsArray.Length == 0) { //moan } else { filterTable.Clear(); foreach (DataRow newRow in filteredRowsArray) { filterTable.ImportRow(newRow); //this seems to be the problem... } dataGridView.DataSource = filterTable; } }
everything is fine up untill i callfilterTable.ImportRow(newRow)
, i think its adding BLANK rows... if i foreach filterTable.Rows the rows are empty :confused: i also triedfilterTable.Rows.Add(newRow);
but then it moans "This row already belongs to another table." anybody hav some ideas? ThanxHarvey Saayman - South Africa Junior Developer .Net, C#, SQL think BIG and kick ASS
you.suck = (you.passion != Programming)
I think instead of using DataRow[] filteredRowsArray = table.Select(filterString); and adding the above row to another table. You should use DataView objdv = table.DefaultView; and then Use objdv.RowFilter = filterstring. It is more easy and fast compared to your above steps. You can get more example on codeproject and google on it. I think it will help u and may solve ur problem.
Sarvesh Upadhyay Senior Software Engineer Birlasoft India Ltd. Microsoft Certified Professional Developer in Dotnet 2.0 Enterprise Application
-
I think instead of using DataRow[] filteredRowsArray = table.Select(filterString); and adding the above row to another table. You should use DataView objdv = table.DefaultView; and then Use objdv.RowFilter = filterstring. It is more easy and fast compared to your above steps. You can get more example on codeproject and google on it. I think it will help u and may solve ur problem.
Sarvesh Upadhyay Senior Software Engineer Birlasoft India Ltd. Microsoft Certified Professional Developer in Dotnet 2.0 Enterprise Application
Hey sarvesh i came across the DataView class just after i posted my messages and did infact use it the way you just said and it worked :) im gona try and use multiple conditions in the filterstring today, ill leave a post if i get stuck again thanx for the reply...
Harvey Saayman - South Africa Junior Developer .Net, C#, SQL think BIG and kick ASS
you.suck = (you.passion != Programming)