why dataview.count become 0 when I double click on a datagrid?

G

Guest

I have a window application. In one of the form, a datagrid has a dataview
as its datasource. Initial filtering result would give the datavew 3 items.
When I double click on the datagrid to edit the selected lie item at which
case I would pop up a separate dialog box to do so, in the debugging code,
the dataview.count would return 0. I get a error message because I tried to
get values out of a dataview that holds 0 items. Does anyone know how do I
get around this problem?

I just want to pass the row that has been double clicked on the datagrid to
another dialog box for user to edit it and save the changes back to the
dataview/dataset.
Thanks, Alpha

private void dgMaintHistory_DoubleClick(object sender, System.EventArgs e)
{
int rowIndex = dgMaintHistory.CurrentRowIndex;
string filter = "ExtVID = " + curVid.ToString() + " and " + "ExtSchItemID
= " + lstSchItem.SelectedValue.ToString();
((DataView)dgMaintHistory.DataSource).RowFilter= filter;
// string miles = dgMaintHistory.CurrentRowIndex

// rowIndex = dsMaint.Tables["ServiceHistory"].
try
{
int count = dvServiceHistory.Count;
string datetest =
dvServiceHistory.Table.Rows[rowIndex]["ServiceDate"].ToString();
// .Table.Rows[rowIndex]["ServiceDate"].ToString;
int shid =
Convert.ToInt32(dvServiceHistory.Table.Rows[rowIndex]["SHistoryID"]);
datetest =
dvServiceHistory.Table.Rows[rowIndex]["ServiceDate"].ToString();
DataTable dtSrvHistory = new DataTable();
dtSrvHistory = dsMaint.Tables["ServiceHistory"];
datetest =
dsMaint.Tables["ServiceHistory"].Rows[rowIndex]["ServiceDate"].ToString();
// datetest =
dvServiceHistory.Table.Rows[rowIndex]["ServiceDate"].ToString();
// dsMaint.Tables["ServiceHistory"].
Form frmEditSrv = new EditServiceHistory(conMaint, dsMaint, shid);
frmEditSrv.ShowDialog();
dgMaintHistory.Refresh();
// ((DataView)lstSchItem.DataSource).RowFilter= strFilter;
frmEditSrv.Dispose();
}
catch(Exception ex)
{
MessageBox.Show("Error gettting the row to be modified." +
ex.ToString(), "Maintenance history error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}
 
B

Bart Mermuys

Hi,

Alpha said:
I have a window application. In one of the form, a datagrid has a dataview
as its datasource. Initial filtering result would give the datavew 3
items.
When I double click on the datagrid to edit the selected lie item at which
case I would pop up a separate dialog box to do so, in the debugging code,
the dataview.count would return 0. I get a error message because I tried
to
get values out of a dataview that holds 0 items. Does anyone know how do
I
get around this problem?

The code is confusing. Why are you filtering a second time to get the
current row ? And if you change the filtering then there is no relation
with the first filtering, so it doens't matter it returns 3 the first time.

Since you're binding to a DataView, you can easily get the current
DataRowView, eg:
DataRowView currentDRV = dvServiceHistory[dgMaintHistory.CurrentRowIndex];

Keep in mind that 'dgMaintHistory.CurrentRowIndex' is always an index into a
DataView and *not* DataTable.Rows. In fact i believe the only way to get the
current index into DataTable.Rows is by enumerating it and checking the id
with the current DataRowView.

But if it fits your needs you can simply pass the current DataRowView to the
detail Form because you can bind the Controls to a DataRowView. Just
remember to call EndEdit or CancelEdit on the DataRowView when the detail
form closes.

HTH,
Greetings
I just want to pass the row that has been double clicked on the datagrid
to
another dialog box for user to edit it and save the changes back to the
dataview/dataset.
Thanks, Alpha

private void dgMaintHistory_DoubleClick(object sender, System.EventArgs e)
{
int rowIndex = dgMaintHistory.CurrentRowIndex;
string filter = "ExtVID = " + curVid.ToString() + " and " + "ExtSchItemID
= " + lstSchItem.SelectedValue.ToString();
((DataView)dgMaintHistory.DataSource).RowFilter= filter;
// string miles = dgMaintHistory.CurrentRowIndex

// rowIndex = dsMaint.Tables["ServiceHistory"].
try
{
int count = dvServiceHistory.Count;
string datetest =
dvServiceHistory.Table.Rows[rowIndex]["ServiceDate"].ToString();
// .Table.Rows[rowIndex]["ServiceDate"].ToString;
int shid =
Convert.ToInt32(dvServiceHistory.Table.Rows[rowIndex]["SHistoryID"]);
datetest =
dvServiceHistory.Table.Rows[rowIndex]["ServiceDate"].ToString();
DataTable dtSrvHistory = new DataTable();
dtSrvHistory = dsMaint.Tables["ServiceHistory"];
datetest =
dsMaint.Tables["ServiceHistory"].Rows[rowIndex]["ServiceDate"].ToString();
// datetest =
dvServiceHistory.Table.Rows[rowIndex]["ServiceDate"].ToString();
// dsMaint.Tables["ServiceHistory"].
Form frmEditSrv = new EditServiceHistory(conMaint, dsMaint, shid);
frmEditSrv.ShowDialog();
dgMaintHistory.Refresh();
// ((DataView)lstSchItem.DataSource).RowFilter= strFilter;
frmEditSrv.Dispose();
}
catch(Exception ex)
{
MessageBox.Show("Error gettting the row to be modified." +
ex.ToString(), "Maintenance history error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}
 
G

Guest

My apology for not cleaning up the code before I posted it. At first I
forgot the datagrid was bind to the dataview and not the dataset. I then
tried getting the current row from the dataview to pass to the detail form
for edit but wasn't able to do so because I wasn't sure of the code to
extract the current, which you've provided for me and I really appreciate it.
I tried to

However, I'm still getting the error becuase the dataview count is showing 0
when there are actually 3 rows listed in the datagrid. It only goes to 0
when I get to the double click module. I'm including the databinding code
and maybe you can help me see what I'm doing worng there.

OK, I finally found the stupid bug. I declare a global and a local dataview
of the same name. The local dvServiceHistory got assigned with data but the
global one never did and that's why the count was 0.

Thank you very much for your help. You were very helpful.
Alpha



private void MaintHistory_Load(object sender, System.EventArgs e)
{
ttHistory.SetToolTip(cmbVName, "Only the vehicles with assigned
maintenance schedule are listed here.");
ttHistory.SetToolTip(txtSchedule, "The maintenance schedule assigned to
the selected vehicle. You can modify this in the Vehicle screen.");
ttHistory.SetToolTip(lstSchItem, "Listing of all the service items
included for the selected Schedule.");
ttHistory.SetToolTip(dgMaintHistory, "All the recorded dates that the
maintenance was performed for the selected service item on the left.");
ttHistory.SetToolTip(btnAdd, "Add a new serive date for the selected
service item.");

cmbVName.DataSource = dsMaint.Tables["VehDetail"];
cmbVName.DisplayMember = "VName";
cmbVName.ValueMember = "VID";
cmbVName.SelectedIndex = 0;
curVid = (int) cmbVName.SelectedValue;
txtSchedule.DataBindings.Add("Text", dsMaint.Tables["VehDetail"],
"ScheduleName");

DataView dvMaintdetail = new DataView(dsMaint.Tables["MaintenanceDetail"],
"VID = " + curVid.ToString(), "ServiceItem ASC",
DataViewRowState.CurrentRows);
lstSchItem.DataSource = dvMaintdetail;
lstSchItem.DisplayMember = "ServiceItem";
lstSchItem.ValueMember = "SchItemID";
lstSchItem.SelectedIndex = 0;
string test = lstSchItem.SelectedValue.ToString();
// int IntValue = Convert.ToInt32(lstSchItem.SelectedValue.ToString());
string filter = "ExtVID = " + curVid.ToString() + " and " + "ExtSchItemID
= " + lstSchItem.SelectedValue.ToString();
DataView dvServiceHistory = new DataView(dsMaint.Tables["ServiceHistory"],
filter, "ServiceDate ASC", DataViewRowState.CurrentRows);

int rc = dvServiceHistory.Count;

dgMaintHistory.DataSource = dvServiceHistory;

DataGridTableStyle tsMaintDetail = new DataGridTableStyle();
tsMaintDetail.MappingName = "ServiceHistory";

DataGridTextBoxColumn colServiceDate = new DataGridTextBoxColumn();
colServiceDate.HeaderText = "Service Date";
colServiceDate.MappingName = "ServiceDate";
colServiceDate.Width = 100;
tsMaintDetail.GridColumnStyles.Add(colServiceDate);

DataGridTextBoxColumn colServiceMileage= new DataGridTextBoxColumn();
colServiceMileage.HeaderText = "Service Mileage";
colServiceMileage.MappingName = "ServiceMileage";
colServiceMileage.Width = 100;
colServiceMileage.Alignment = HorizontalAlignment.Right;
tsMaintDetail.GridColumnStyles.Add(colServiceMileage);

dgMaintHistory.TableStyles.Add(tsMaintDetail);
rc = dvServiceHistory.Count;
Loading = false;

}





Bart Mermuys said:
Hi,

Alpha said:
I have a window application. In one of the form, a datagrid has a dataview
as its datasource. Initial filtering result would give the datavew 3
items.
When I double click on the datagrid to edit the selected lie item at which
case I would pop up a separate dialog box to do so, in the debugging code,
the dataview.count would return 0. I get a error message because I tried
to
get values out of a dataview that holds 0 items. Does anyone know how do
I
get around this problem?

The code is confusing. Why are you filtering a second time to get the
current row ? And if you change the filtering then there is no relation
with the first filtering, so it doens't matter it returns 3 the first time.

Since you're binding to a DataView, you can easily get the current
DataRowView, eg:
DataRowView currentDRV = dvServiceHistory[dgMaintHistory.CurrentRowIndex];

Keep in mind that 'dgMaintHistory.CurrentRowIndex' is always an index into a
DataView and *not* DataTable.Rows. In fact i believe the only way to get the
current index into DataTable.Rows is by enumerating it and checking the id
with the current DataRowView.

But if it fits your needs you can simply pass the current DataRowView to the
detail Form because you can bind the Controls to a DataRowView. Just
remember to call EndEdit or CancelEdit on the DataRowView when the detail
form closes.

HTH,
Greetings
I just want to pass the row that has been double clicked on the datagrid
to
another dialog box for user to edit it and save the changes back to the
dataview/dataset.
Thanks, Alpha

private void dgMaintHistory_DoubleClick(object sender, System.EventArgs e)
{
int rowIndex = dgMaintHistory.CurrentRowIndex;
string filter = "ExtVID = " + curVid.ToString() + " and " + "ExtSchItemID
= " + lstSchItem.SelectedValue.ToString();
((DataView)dgMaintHistory.DataSource).RowFilter= filter;
// string miles = dgMaintHistory.CurrentRowIndex

// rowIndex = dsMaint.Tables["ServiceHistory"].
try
{
int count = dvServiceHistory.Count;
string datetest =
dvServiceHistory.Table.Rows[rowIndex]["ServiceDate"].ToString();
// .Table.Rows[rowIndex]["ServiceDate"].ToString;
int shid =
Convert.ToInt32(dvServiceHistory.Table.Rows[rowIndex]["SHistoryID"]);
datetest =
dvServiceHistory.Table.Rows[rowIndex]["ServiceDate"].ToString();
DataTable dtSrvHistory = new DataTable();
dtSrvHistory = dsMaint.Tables["ServiceHistory"];
datetest =
dsMaint.Tables["ServiceHistory"].Rows[rowIndex]["ServiceDate"].ToString();
// datetest =
dvServiceHistory.Table.Rows[rowIndex]["ServiceDate"].ToString();
// dsMaint.Tables["ServiceHistory"].
Form frmEditSrv = new EditServiceHistory(conMaint, dsMaint, shid);
frmEditSrv.ShowDialog();
dgMaintHistory.Refresh();
// ((DataView)lstSchItem.DataSource).RowFilter= strFilter;
frmEditSrv.Dispose();
}
catch(Exception ex)
{
MessageBox.Show("Error gettting the row to be modified." +
ex.ToString(), "Maintenance history error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}
 
Top