For my webapp, I am getting data in a DS and binding it to a GridView as follows:
public void display_grid(String SQL)
{
SQL = SQL.Replace(SQL_DATE_TOKEN_ID, this.Calendar.SelectedDate.ToString());
oData.PopulateDataset(oDataSet._getData.TableName, SQL, oDataSet);
oDataSet.AcceptChanges();
GridView1.DataSource = oDataSet;
GridView1.DataBind();
}
The GridView has a checkbox field. The functionality that I want to implement is - the checked rows should be displayed in another grid and deleted from the first one. I am able to get the selected rows to be added to the other grid but when I am deleting from DS and rebinding the GridView am getting an error as the count in the DS gets set to 0 as soon as I leave the display_grid method.
public void loadDataTable()
{
System.Diagnostics. Debug.Write("Entering loadDataTable method - Dataset row count = " + oDataSet._getData.Rows.Count.ToString());
int i = 0;
int j = 0;
int totRow = GridView1.Rows.Count;
int totCol = GridView1.Rows[0].Cells.Count;
DataTable dt = new DataTable();
// Set the table headers
string[] arrHeaders = {"Select","Product_Id","Count","Run_Id" };
// Get the table headers
for (j = 0; j < totCol; j++)
{
dt.Columns.Add( new DataColumn(arrHeaders[j].ToString(), typeof(string)));
}
DataRow dr;
for (i = 0; i < totRow; i++)
{
GridViewRow row = GridView1.Rows;
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
if (isChecked)
{
dr = dt.NewRow();
for (j = 0; j < totCol; j++)
{
dr[j] = GridView1.Rows.Cells[j].Text;
}
dt.Rows.Add(dr);
deleteRow(i);
}
}
fillStatusGridView(dt);
}
public void deleteRow(int i)
{
System.Diagnostics. Debug.Write("Entering loadDataTable method - Dataset row count = " + oDataSet._getData.Rows.Count.ToString());
oDataSet._getData.Rows.Delete(); //when the control reaches here, I get an error - There is no row at position i
oDataSet.AcceptChanges();
GridView1.DataSource = oDataSet;
GridView1.DataBind();
}
protected void fillStatusGridView(DataTable dt)
{
if (dt.Rows.Count > 0)
{
StatusGridView.DataSource = dt;
StatusGridView.DataBind();
}
else
{
dt.Rows.Add(dt.NewRow());
StatusGridView.DataSource = dt;
StatusGridView.DataBind();
int TotalColumns = StatusGridView.Rows[0].Cells.Count;
StatusGridView.Rows[0].Cells.Clear();
StatusGridView.Rows[0].Cells.Add( new TableCell());
StatusGridView.Rows[0].Cells[0].ColumnSpan = TotalColumns;
StatusGridView.Rows[0].Cells[0].Text = "No Record Found";
}
}
I am able to find several examples where we have a delete button for each row and then kick off a rowdeleted event. But my condition is to delete the checked rows.
Can somebody please help me to identify what I am doing wrong here. Any help is greatly appreciated.
Thanks | | Saesha Friday, April 04, 2008 12:57 PM | Hi,
Take a look on this sample i created for you. I have here a class to create my DataSet data.
class Student
{
public int Id;
public string Name;
}
At my main class i have this code.
public partial class SampleForm: Form
{
public SampleForm()
{
int rows = 0;
Student[] students = {
new Student { Id = 1, Name = "Joe Rattz" },
new Student { Id = 6, Name = "Ulyses Hutchens" },
new Student { Id = 19, Name = "Bob Tanko" },
new Student { Id = 45, Name = "Erin Doutensal" },
new Student { Id = 1, Name = "Joe Rattz" },
new Student { Id = 12, Name = "Bob Mapplethorpe" },
new Student { Id = 17, Name = "Anthony Adams" },
new Student { Id = 32, Name = "Dignan Stephens" }
};
//Focus on this area
ds = GetDataTable(students); // apply here your bounded method
rows = ds.Tables[0].Rows.Count; // count here is 8
ds.Tables[0].Rows[0].Delete(); // rows here is deleted.
rows = ds.Tables[0].Rows.Count; // here is 7
}
static DataSet GetDataTable(Student[] students)
{
DataSet table = new DataSet();
table.Tables.Add("Students");
table.Tables[0].Columns.Add("Id", typeof(Int32));
table.Tables[0].Columns.Add("Name", typeof(string));
foreach (Student student in students)
table.Tables[0].Rows.Add(student.Id, student.Name);
return (table);
}
} | | Bermil M. Espina Saturday, April 05, 2008 2:24 AM | if you are using check box to determine what row/s to delete then you need to apply for loop in order to delete items with a checkbox being checked.
at your for loop above you need to change the behavior in your deleteRow(int i) method. what i see here is you are attempting to delete a row in the gridview but not really deleting the actual data from where it is bounded from. try to delete what is in the datatable.
try to use
dt.Rows[ i ].Delete();
Good Luck.
| | Bermil M. Espina Friday, April 04, 2008 1:36 PM | Bermil,
I appreciate your response.Actually, I am deleting the data from where it is bounded i.e. a dataset. The data in GridView1 is coming from DS oDataSet, table _getData. The problem is when I am trying to delete the row from the DS its giving me an error saying " There is no row at the position specified" and when I look, the DS has a rowcount of 0. Somehow the DS is getting cleared of the population.
Thanks, | | Saesha Friday, April 04, 2008 2:42 PM | Hi,
Take a look on this sample i created for you. I have here a class to create my DataSet data.
class Student
{
public int Id;
public string Name;
}
At my main class i have this code.
public partial class SampleForm: Form
{
public SampleForm()
{
int rows = 0;
Student[] students = {
new Student { Id = 1, Name = "Joe Rattz" },
new Student { Id = 6, Name = "Ulyses Hutchens" },
new Student { Id = 19, Name = "Bob Tanko" },
new Student { Id = 45, Name = "Erin Doutensal" },
new Student { Id = 1, Name = "Joe Rattz" },
new Student { Id = 12, Name = "Bob Mapplethorpe" },
new Student { Id = 17, Name = "Anthony Adams" },
new Student { Id = 32, Name = "Dignan Stephens" }
};
//Focus on this area
ds = GetDataTable(students); // apply here your bounded method
rows = ds.Tables[0].Rows.Count; // count here is 8
ds.Tables[0].Rows[0].Delete(); // rows here is deleted.
rows = ds.Tables[0].Rows.Count; // here is 7
}
static DataSet GetDataTable(Student[] students)
{
DataSet table = new DataSet();
table.Tables.Add("Students");
table.Tables[0].Columns.Add("Id", typeof(Int32));
table.Tables[0].Columns.Add("Name", typeof(string));
foreach (Student student in students)
table.Tables[0].Rows.Add(student.Id, student.Name);
return (table);
}
} | | Bermil M. Espina Saturday, April 05, 2008 2:24 AM | Thanks Bermil,
I was able to finally figure out why the DS was nto being saved. Since the page was posted again when the grid was being displayed, the DS was losing its changes. When I used Viewstate, I was able to maintain and access the DS later. | | Saesha Friday, April 11, 2008 3:01 PM |
|