problem with refreshing a datagrid

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

IDE: VS 2003 :NET
OS: XP Pro

My app have a form with a tab-control on it. The tab-control have 2
tabpages. One of the tabpages displays a datagrid, and the other tabpage
displays details (order date, name, address etc) about the selected row in
the datagrid...

My problem is when I enter a new record in the details tabpage (saves data
to database), and go back to the datagrid. Only the data from the PM-table
(the PM-table, see below) is visible


This is the code I have in the forms constructor:
SqlCommand cmdStatus = sqlCon.CreateCommand();
cmdStatus.CommandText = "Select p.pm_Id, l.loc_Code, a.as_Code,
p.pm_Description, p.pm_LastDone " +
"from PM p, Location l, Assets a where p.pm_Location = l.loc_Id and
p.pm_Asset = a.as_Id order by
..pm_LastDone";
cmdStatus.CommandType = CommandType.Text;
adapter.SelectCommand = cmdStatus;
mainTable = "PM";

The columns l.loc_Code and a.as_Code aren't updated/refresehed... I have
looked into the database, everything is worker there... the PM table have
got a pm_Location value and a pm_Asset value

I have experimented with using datagrid.update()... but it doesn't solve the
problem...

How do I correct this problem?

Jeff
 
Hello Sijin!

I've changed the code a bit, but still the same problem

The difference is that cmdPM.CommandText selects data from a view and not
from tables as in my first post!

SqlCommand cmdPM = sqlCon.CreateCommand();
cmdPM.CommandText = "select * from vwPMlist";
cmdPM.CommandType = CommandType.Text;
adapter.SelectCommand = cmdPM;
mainTable = "vwPMlist"; //mainTable just hold the name of the
view
adapter.Fill(ds,"vwPMlist"); //ds is of type DataSet
//data binding:--- I'm a newbie... this is all the databinding I do:
dgList.DataSource = ds; //dgList is the datagrid
dgList.DataMember = mainTable;

********

This is the script that saves the data entered in the other tabpage
SqlCommand cmd = sqlCon.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO PM (pm_location, pm_Asset, pm_description)
VALUES (@pm_location,
pm_asset, @pm_Description)";

cmd.Parameters.Add("@pm_location", SqlDbType.Decimal, 9, "pm_location");
cmd.Parameters.Add("@pm_asset", SqlDbType.Decimal, 9, "pm_asset");
cmd.Parameters.Add("@pm_description", SqlDbType.VarChar, 150,
"pm_description");
adapter.InsertCommand = cmd;

DataRow dr = ds.Tables[mainTable].NewRow();
dr[2] = txtLocationID.Text;
dr[4] = txtAsset.Text;
dr[5] = txtDescription.Text;
try
{
ds.Tables["vwPMlist"].Rows.Add(dr);
}
catch (ArgumentException er)
{
MessageBox.Show(er.Message);
}
adapter.Update(ds, mainTable); //adapter is my SqlDataAdapter variable
 
Hi Jeff,

Just to confirm,

In Tabpage 1, you have a datarid which shows all columns from a dataview
called vwPMList. In Tabpage 2 you show the details of the row that is
selected in the datagrid.

Now in the second tabpage, you are inserting a new row into the dataset,
and then saving the changes to the database which is working ok i.e. the
new row is getting saved to the DB, but it is not being displayed in the
datagrid on tabpage 1.

Based on this info, i think what you need to do is refresh the datagrid.
You can do this by calling the Refresh method on the CurrencyManager
of the DataGrid.

CurrencyManager cm = (CurrencyManager)dgList.BindingContext[ds,mainTable];
cm.Refresh();

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

Hello Sijin!

I've changed the code a bit, but still the same problem

The difference is that cmdPM.CommandText selects data from a view and not
from tables as in my first post!

SqlCommand cmdPM = sqlCon.CreateCommand();
cmdPM.CommandText = "select * from vwPMlist";
cmdPM.CommandType = CommandType.Text;
adapter.SelectCommand = cmdPM;
mainTable = "vwPMlist"; //mainTable just hold the name of the
view
adapter.Fill(ds,"vwPMlist"); //ds is of type DataSet
//data binding:--- I'm a newbie... this is all the databinding I do:
dgList.DataSource = ds; //dgList is the datagrid
dgList.DataMember = mainTable;

********

This is the script that saves the data entered in the other tabpage
SqlCommand cmd = sqlCon.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO PM (pm_location, pm_Asset, pm_description)
VALUES (@pm_location,
pm_asset, @pm_Description)";

cmd.Parameters.Add("@pm_location", SqlDbType.Decimal, 9, "pm_location");
cmd.Parameters.Add("@pm_asset", SqlDbType.Decimal, 9, "pm_asset");
cmd.Parameters.Add("@pm_description", SqlDbType.VarChar, 150,
"pm_description");
adapter.InsertCommand = cmd;

DataRow dr = ds.Tables[mainTable].NewRow();
dr[2] = txtLocationID.Text;
dr[4] = txtAsset.Text;
dr[5] = txtDescription.Text;
try
{
ds.Tables["vwPMlist"].Rows.Add(dr);
}
catch (ArgumentException er)
{
MessageBox.Show(er.Message);
}
adapter.Update(ds, mainTable); //adapter is my SqlDataAdapter variable


Can you show how you are setting up your databiding?

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph



in

data

PM-table

have

the
 
Thank you for your reply!

But still it doesn't work...

the vwPMlist is not based on the DataView class, it's just a view in the
database....
(create view .....) below is the select statement for the view
SELECT TOP 100 PERCENT p.pm_Id, l.loc_Code, p.pm_location, a.as_Code,
p.pm_Asset, p.pm_Description, p.pm_LastDone
FROM dbo.PM p INNER JOIN
dbo.Location l ON p.pm_location = l.loc_Id INNER JOIN
dbo.Assets a ON p.pm_Asset = a.as_Id
ORDER BY p.pm_LastDone

I've added some debugging information to the code:
Debug.WriteLine("===TAB CONTROL==================================");
CurrencyManager m = (CurrencyManager) this.Controls[0].BindingContext[ds];
foreach (Binding b in m.Bindings)
{
Debug.WriteLine("Control = " + b.Control.Name);
}

Debug.WriteLine("===FORM CONTROL=================================");
m = (CurrencyManager) this.BindingContext[ds];
foreach (Binding b in m.Bindings)
{
Debug.WriteLine("Control = " + b.Control.Name);
}

Debug.WriteLine("===FORM CONTROL BindingManagerBase=============");
BindingManagerBase mybind = this.BindingContext[ds, "vwPMlist"];
foreach (Binding b in mybind.Bindings)
{
Debug.WriteLine("Control = " + b.Control.ToString());
}

Debug.WriteLine("===TAB CONTROL BindingManagerBase=============");
mybind = this.Controls[0].BindingContext[ds, "vwPMlist"];
foreach (Binding b in mybind.Bindings)
{
Debug.WriteLine("Control = " + b.Control.ToString());
}

Debug.WriteLine("===TAB page BindingManagerBase=============");
//this is the tabpage that don't have the datagrid
mybind = this.Controls[0].Controls[0].BindingContext[ds, "vwPMlist"];
foreach (Binding b in mybind.Bindings)
{
Debug.WriteLine("Control = " + b.Control.ToString());
}

Non of these Debug.WriteLine are written to the Output window, except for
"===...."

This script below is my code for binding the datasource to my textboxes in
the tabpage, displaying details:
txtDescription.DataBindings.Add("Text", ds.Tables["vwPMlist"],
"pm_Description");
txtLocation.DataBindings.Add("Text", ds.Tables["vwPMlist"], "loc_Code");
txtAsset.DataBindings.Add("Text", ds.Tables["vwPMlist"], "pm_Asset");
txtLocationID.DataBindings.Add("Text", ds.Tables["vwPMlist"],
"pm_Location");

After saving, the columns pm_location and pm_asset have the value (null).
But if I close the form and reopen it, I can see that these columns got some
proper values (So data for the columns are saved to the database)





Sijin Joseph said:
Hi Jeff,

Just to confirm,

In Tabpage 1, you have a datarid which shows all columns from a dataview
called vwPMList. In Tabpage 2 you show the details of the row that is
selected in the datagrid.

Now in the second tabpage, you are inserting a new row into the dataset,
and then saving the changes to the database which is working ok i.e. the
new row is getting saved to the DB, but it is not being displayed in the
datagrid on tabpage 1.

Based on this info, i think what you need to do is refresh the datagrid.
You can do this by calling the Refresh method on the CurrencyManager
of the DataGrid.

CurrencyManager cm = (CurrencyManager)dgList.BindingContext[ds,mainTable];
cm.Refresh();

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

Hello Sijin!

I've changed the code a bit, but still the same problem

The difference is that cmdPM.CommandText selects data from a view and not
from tables as in my first post!

SqlCommand cmdPM = sqlCon.CreateCommand();
cmdPM.CommandText = "select * from vwPMlist";
cmdPM.CommandType = CommandType.Text;
adapter.SelectCommand = cmdPM;
mainTable = "vwPMlist"; //mainTable just hold the name of the
view
adapter.Fill(ds,"vwPMlist"); //ds is of type DataSet
//data binding:--- I'm a newbie... this is all the databinding I do:
dgList.DataSource = ds; //dgList is the datagrid
dgList.DataMember = mainTable;

********

This is the script that saves the data entered in the other tabpage
SqlCommand cmd = sqlCon.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO PM (pm_location, pm_Asset, pm_description)
VALUES (@pm_location,
pm_asset, @pm_Description)";

cmd.Parameters.Add("@pm_location", SqlDbType.Decimal, 9, "pm_location");
cmd.Parameters.Add("@pm_asset", SqlDbType.Decimal, 9, "pm_asset");
cmd.Parameters.Add("@pm_description", SqlDbType.VarChar, 150,
"pm_description");
adapter.InsertCommand = cmd;

DataRow dr = ds.Tables[mainTable].NewRow();
dr[2] = txtLocationID.Text;
dr[4] = txtAsset.Text;
dr[5] = txtDescription.Text;
try
{
ds.Tables["vwPMlist"].Rows.Add(dr);
}
catch (ArgumentException er)
{
MessageBox.Show(er.Message);
}
adapter.Update(ds, mainTable); //adapter is my SqlDataAdapter variable


Can you show how you are setting up your databiding?

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph



Jeff wrote:

IDE: VS 2003 :NET
OS: XP Pro

My app have a form with a tab-control on it. The tab-control have 2
tabpages. One of the tabpages displays a datagrid, and the other tabpage
displays details (order date, name, address etc) about the selected
row

in
the datagrid...

My problem is when I enter a new record in the details tabpage (saves
data

to database), and go back to the datagrid. Only the data from the
PM-table

(the PM-table, see below) is visible


This is the code I have in the forms constructor:
SqlCommand cmdStatus = sqlCon.CreateCommand();
cmdStatus.CommandText = "Select p.pm_Id, l.loc_Code, a.as_Code,
p.pm_Description, p.pm_LastDone " +
"from PM p, Location l, Assets a where p.pm_Location = l.loc_Id and
p.pm_Asset = a.as_Id order by
.pm_LastDone";
cmdStatus.CommandType = CommandType.Text;
adapter.SelectCommand = cmdStatus;
mainTable = "PM";

The columns l.loc_Code and a.as_Code aren't updated/refresehed... I have
looked into the database, everything is worker there... the PM table
have

got a pm_Location value and a pm_Asset value

I have experimented with using datagrid.update()... but it doesn't
solve

the
problem...

How do I correct this problem?

Jeff
 
Hello Sijin!

I created another form that is not using tab-control..... Just to verify
that tab-control isn't the problem....

I attached the source for this test form to this post. This form have the
same problem

When I click on the Save-button in the form, and save data entered in the
textboxes, the columns "loc_Code" and "as_Code" in the datagrid get the
value "(null)"... But after I close the form and reopen it, the columns have
got the correct value.

These two columns ("loc_Code" and "as_Code") are lookup columns, They
displays more readable information than the colums "pm_Location" and
"pm_Asset".
SELECT TOP 100 PERCENT p.pm_Id, l.loc_Code, p.pm_location, a.as_Code,
p.pm_Asset, p.pm_Description, p.pm_LastDone
FROM dbo.PM p INNER JOIN
dbo.Location l ON p.pm_location = l.loc_Id INNER JOIN
dbo.Assets a ON p.pm_Asset = a.as_Id
ORDER BY p.pm_LastDone




Sijin Joseph said:
Hi Jeff,

Just to confirm,

In Tabpage 1, you have a datarid which shows all columns from a dataview
called vwPMList. In Tabpage 2 you show the details of the row that is
selected in the datagrid.

Now in the second tabpage, you are inserting a new row into the dataset,
and then saving the changes to the database which is working ok i.e. the
new row is getting saved to the DB, but it is not being displayed in the
datagrid on tabpage 1.

Based on this info, i think what you need to do is refresh the datagrid.
You can do this by calling the Refresh method on the CurrencyManager
of the DataGrid.

CurrencyManager cm = (CurrencyManager)dgList.BindingContext[ds,mainTable];
cm.Refresh();

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

Hello Sijin!

I've changed the code a bit, but still the same problem

The difference is that cmdPM.CommandText selects data from a view and not
from tables as in my first post!

SqlCommand cmdPM = sqlCon.CreateCommand();
cmdPM.CommandText = "select * from vwPMlist";
cmdPM.CommandType = CommandType.Text;
adapter.SelectCommand = cmdPM;
mainTable = "vwPMlist"; //mainTable just hold the name of the
view
adapter.Fill(ds,"vwPMlist"); //ds is of type DataSet
//data binding:--- I'm a newbie... this is all the databinding I do:
dgList.DataSource = ds; //dgList is the datagrid
dgList.DataMember = mainTable;

********

This is the script that saves the data entered in the other tabpage
SqlCommand cmd = sqlCon.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO PM (pm_location, pm_Asset, pm_description)
VALUES (@pm_location,
pm_asset, @pm_Description)";

cmd.Parameters.Add("@pm_location", SqlDbType.Decimal, 9, "pm_location");
cmd.Parameters.Add("@pm_asset", SqlDbType.Decimal, 9, "pm_asset");
cmd.Parameters.Add("@pm_description", SqlDbType.VarChar, 150,
"pm_description");
adapter.InsertCommand = cmd;

DataRow dr = ds.Tables[mainTable].NewRow();
dr[2] = txtLocationID.Text;
dr[4] = txtAsset.Text;
dr[5] = txtDescription.Text;
try
{
ds.Tables["vwPMlist"].Rows.Add(dr);
}
catch (ArgumentException er)
{
MessageBox.Show(er.Message);
}
adapter.Update(ds, mainTable); //adapter is my SqlDataAdapter variable


Can you show how you are setting up your databiding?

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph



Jeff wrote:

IDE: VS 2003 :NET
OS: XP Pro

My app have a form with a tab-control on it. The tab-control have 2
tabpages. One of the tabpages displays a datagrid, and the other tabpage
displays details (order date, name, address etc) about the selected
row

in
the datagrid...

My problem is when I enter a new record in the details tabpage (saves
data

to database), and go back to the datagrid. Only the data from the
PM-table

(the PM-table, see below) is visible


This is the code I have in the forms constructor:
SqlCommand cmdStatus = sqlCon.CreateCommand();
cmdStatus.CommandText = "Select p.pm_Id, l.loc_Code, a.as_Code,
p.pm_Description, p.pm_LastDone " +
"from PM p, Location l, Assets a where p.pm_Location = l.loc_Id and
p.pm_Asset = a.as_Id order by
.pm_LastDone";
cmdStatus.CommandType = CommandType.Text;
adapter.SelectCommand = cmdStatus;
mainTable = "PM";

The columns l.loc_Code and a.as_Code aren't updated/refresehed... I have
looked into the database, everything is worker there... the PM table
have

got a pm_Location value and a pm_Asset value

I have experimented with using datagrid.update()... but it doesn't
solve

the
problem...

How do I correct this problem?

Jeff
 
Back
Top