473,324 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

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);
}

}
Nov 17 '05 #1
2 3255
Hi,

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:C2**********************************@microsof t.com...
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);
}

}

Nov 17 '05 #2
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(colServiceMilea ge);

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

}

"Bart Mermuys" wrote:
Hi,

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:C2**********************************@microsof t.com...
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);
}

}


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Martin Schmid | last post by:
I am trying to implement a DataView for a DataGrid so I can sort at runtime by clicking on column headers. My initial page load works... it displays the data However, when I click a column...
3
by: Rxd | last post by:
I have a Datagrid that should have a couple of hidden columns. I used a DataGridTableStyle to hide the columns and it was working fine except I needed to prevent the DataGrid from allowing new rows...
3
by: Randy | last post by:
Hello All, I'm trying to use a DataView and in the rowfilter, base it on two fields in the table. I can't find any examples on this. Is there a way to do it. I'm talking about something like......
2
by: Ed_P. | last post by:
Hello, I have a situation that I wish some help with. I have a DataSet object with DataTables populated from a ms access database. Each DataTable has a RunLogTitle. I have a TreeView object...
13
by: Steve | last post by:
I have a form with a dataset and a datagrid. I created a dataview on this dataset. When the user modifies the datagrid, I look up this record in the dataview to make sure it is unique. Here is...
17
by: A_PK | last post by:
I have problem databinding the DataGrid with DataView/DataSet after the filter... I create the following proceudre in order for user to filter as many as they want, but the following code is only...
5
by: enceladus311 | last post by:
I'm trying to find a way to keep from having to fill a DataView after every PostBack to a page. Basically, the design is that I have a DataView that I fill, which I then set as the DataSource to a...
0
by: Nathan Franklin | last post by:
Hello Guys, I have been trying to work this our for so long, but I just can't seem to find the answer. I am loading a datatable from a an access database using an oledbdataadapter. I then...
10
by: babis | last post by:
I work with .NET2003 C# windows form application, i add a datagrid in my form like this: dataGridProduct.DataSource = dataViewProduct; I also make the allowSort property of the datagrid...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.