|
-
Jul 31st, 2007, 08:54 AM
#1
Thread Starter
Fanatic Member
Refreshing Parents datagrid
I have a form (frmPrintReq) with a datagrid on it. When the user double clicks a row, it opens a second form (frmShowReq) with the information of that row. The user can then update the information. When the user clicks the update button of form2, how can I then update the dataset (dsNew1) of the datagrid (dgNew) on form1 and refresh the datagrid. Here is the code from the first form that opens the second form:
Code:
private void frmPrintReq_Load(object sender, EventArgs e)
{
sqlDataAdapter1.Fill(dsNew1, "MyImages");
}
private void dgNew_DoubleClick(object sender, EventArgs e)
{
if (this.dgNew.CurrentRowIndex.ToString() == "-1")
{
MessageBox.Show("First select a print request", "No request selected.", MessageBoxButtons.OK);
}
else
{
//get collectionid number
DataGridCell myCell = new DataGridCell();
myCell.RowNumber = dgNew.CurrentRowIndex;
myCell.ColumnNumber = 4;
Main.Req.strReq = Convert.ToString(dgNew[myCell]);
//open form showing the request
// Create a new instance of the child form.
frmShowReq childForm = new frmShowReq();
// Make it a child of this MDI form before showing it.
childForm.MdiParent = this.MdiParent;
childForm.Text = "IO Codes";
childForm.Show();
}
}
and here is the code from the update button on form2
Code:
private void btnUpdate_Click(object sender, EventArgs e)
{
//CHeck to see if there are any items
if (txtDocID.Text.Length < 0)
{
MessageBox.Show("There is no print job selected to update.", "Error!");
}
else
{
try
{
//Update the record to Approved
myConnection = new SqlConnection("workstation id=S-KG;packet size=4096;integrated security=SSPI;data source=SCE;persist security info=False;initial catalog=ELibrary");
string cmdtext = "Update PrintJobs SET Number = @Number, Method = @Method, DueDate = @DueDate, CoverColor = @CoverColor, Color = @Color, Instructions = @Instructions, Name = @Name, Phone = @Phone, Contact = @Contact, IO = @IO, Status = @Status where PrintID = '" + id + "'";
SqlCommand sqlcmd;
sqlcmd = new SqlCommand(cmdtext, myConnection);
sqlcmd.Parameters.AddWithValue("@Number", cboNumber.Text);
sqlcmd.Parameters.AddWithValue("@Method", cboMethod.Text);
sqlcmd.Parameters.AddWithValue("@DueDate", dtDue.Text);
sqlcmd.Parameters.AddWithValue("@CoverColor", cboCoverColor.Text);
sqlcmd.Parameters.AddWithValue("@Color", cboColor.Text);
sqlcmd.Parameters.AddWithValue("@Instructions", txtInstructions.Text);
sqlcmd.Parameters.AddWithValue("@Name", txtName.Text);
sqlcmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
sqlcmd.Parameters.AddWithValue("@Contact", chkContact.Checked.ToString());
sqlcmd.Parameters.AddWithValue("@IO", cboIO.Text);
sqlcmd.Parameters.AddWithValue("@Status", cboStatus.Text);
myConnection.Open();
sqlcmd.ExecuteNonQuery();
MessageBox.Show("Print job has been successfully changed.", "Print job changed", MessageBoxButtons.OK);
myConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error!");
myConnection.Close();
}
}
}
-
Jul 31st, 2007, 08:11 PM
#2
Re: Refreshing Parents datagrid
I addressed this very same issue for someone else just recently, although in VB.
http://www.vbforums.com/showthread.php?t=481574
-
Aug 1st, 2007, 05:40 AM
#3
Thread Starter
Fanatic Member
Re: Refreshing Parents datagrid
I am dealing with 2.0. I have never heard of CurrencyManager but from what I gather, that is what I need to use? Currently, I have form2 update the sql database directly and when I go back to the first form, the user has to click the refresh data button, that I added. It would be probably better coding to update the dataset and have that update the database and then when switching back have form1 refresh automatically but I have no idea on how to accomplish that.
When I drop a SQLDataAdapter on a form and run through the Wizard and then generate a dataset, is that dataset available to other forms?
Last edited by Beast777; Aug 1st, 2007 at 06:04 AM.
-
Aug 1st, 2007, 06:26 AM
#4
Re: Refreshing Parents datagrid
Well, for start you should have specified that you were using 2.0. I assumed it was 1.x because you're using a DataGrid. Is there a reason you're not using a DataGridView? Anyway, you have the BindingSource class at your disposal so you can just follow the link above, then follow the link in that thread to my CodeBank thread and use it directly. The CurrencyManager was only required because 1.x doesn't have the BindingSource, which I specifically stated in that thread.
-
Aug 1st, 2007, 06:59 AM
#5
Thread Starter
Fanatic Member
Re: Refreshing Parents datagrid
I have added this to the top of form 1. Will that allow me to access the dataset from form2?
Code:
namespace ELibrary
{
public partial class frmPrintReq : Form
{
private static dsNew newInstance;
public frmPrintReq()
{
InitializeComponent();
}
private void frmPrintReq_Load(object sender, EventArgs e)
{
sqlDataAdapter1.Fill(dsNew1, "MyImages");
sqlDataAdapter2.Fill(inProgress1, "MyImages");
sqlDataAdapter3.Fill(dsComplete1, "MyImages");
}
public static dsNew dsNew1instance
{
get { return frmPrintReq.newInstance; }
set { frmPrintReq.newInstance=value;}
}
-
Aug 1st, 2007, 07:01 AM
#6
Thread Starter
Fanatic Member
Re: Refreshing Parents datagrid
Sorry, I moved from 1.0 to 2.0 not that long ago. So I take it that datagrids are a thing of the past in 2.0 and datagridviews are the new in.
-
Aug 1st, 2007, 07:35 AM
#7
Re: Refreshing Parents datagrid
If you've moved an existing project from 1.x to 2.0 then you would, of course, retained the existing controls. The DataGridView is superior to the DataGrid in almost every way so you should use it in all new work and, if it's practical, replace existing DataGrid controls with DataGridViews. That would require some code changes too so it may not be practical. Even if you keep your DataGrids you can still add a BindingSource between the data and the control. A BindingSource is basically just a convenient wrapper for a CurrencyManager, which you previously had to use directly.
-
Aug 1st, 2007, 08:27 AM
#8
Thread Starter
Fanatic Member
Re: Refreshing Parents datagrid
Actually I have never used Currency Manager as I have not really done a project like this one before. All my work is going to be in VS2005 2.0 now, so using Datagridviews will be the way I go. I have replaced the datagrid with a datagridview and its working, now I am still trying to figure out how to get form2 to use the same dataset? or bindingsource?
-
Aug 1st, 2007, 09:08 AM
#9
Thread Starter
Fanatic Member
Re: Refreshing Parents datagrid
Maybe I going about this wrong but I added this to the top
Code:
namespace ELibrary
{
public partial class frmPrintReq : Form
{
private BindingSource bsNew;
public frmPrintReq()
{
InitializeComponent();
}
and then while filling the dataset I added this:
Code:
private void frmPrintReq_Load(object sender, EventArgs e)
{
sqlDataAdapter1.Fill(dsNew1, "MyImages");
sqlDataAdapter2.Fill(inProgress1, "MyImages");
sqlDataAdapter3.Fill(dsComplete1, "MyImages");
bsNew.DataSource = dsNew1;
}
Will that allow me to access that binding source from the second form? And if so what can I do with it?
-
Aug 1st, 2007, 05:47 PM
#10
Re: Refreshing Parents datagrid
You should not be trying to use the same BindingSource in another form. You pass the data itself from the first form to the second. If you want to use data-binding in the second form then you use another BindingSource. If you're using the dialogue to edit a single record then I suggest not using data-binding.
As I have shown in that original CodeBank thread (you have looked at it, right?) you should pass just the specific DataRow you want to edit and then load its field values into the appropriate controls. Only when the user presses OK do you save the new values to the row, which allows the user to press Cancel and never have any changes made.
If the user does press OK and you save the new values to the row then the first form will update automatically because that same DataRow object is still bound to it.
-
Aug 2nd, 2007, 07:55 AM
#11
Thread Starter
Fanatic Member
Re: Refreshing Parents datagrid
I read it, but I have a few problems converting it to C# to try it.
Do I just need to use this:
Dim row As DataRow = DirectCast(this.BindingSource1.Current, DataRowView).Row
and then somehow pass that row to the second form?
Also, I have never used Dialogue, I have also passed the row id and then opened another connection to the database that opens that row and then I bind each control to my data from the datareader. Obviously, I am doing it the hard way. Normally I would be editting one row of data but in this case, when the user clicks on the item, it brings up all items in that order in a listview and then when the user clicks on the listview, they can edit any of the items in the order in databound controls below. I will post the code below for both forms, please feel free to comment on them.
If I drop a SqlDataAdapter on the form and go thru the wizard, generate the dataset and then bind the datagridview to the dataset, where does the bindingsource come in?
Sorry about all the questions, but I would like to get this figured out the proper way and then I can code better.
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
Code:
namespace ELibrary
{
public partial class frmPrintReq : Form
{
public frmPrintReq()
{
InitializeComponent();
}
private void frmPrintReq_Load(object sender, EventArgs e)
{
sqlDataAdapter1.Fill(dsNew1, "MyImages");
sqlDataAdapter2.Fill(inProgress1, "MyImages");
sqlDataAdapter3.Fill(dsComplete1, "MyImages");
}
private void dgInProgress_CurrentCellChanged(object sender, EventArgs e)
{
if (this.dgInProgress.CurrentRowIndex.ToString() == "-1")
{
MessageBox.Show("First select a print request", "No request selected.", MessageBoxButtons.OK);
}
else
{
//get collectionid number
DataGridCell myCell = new DataGridCell();
myCell.RowNumber = dgInProgress.CurrentRowIndex;
myCell.ColumnNumber = 4;
Main.Req.strReq = Convert.ToString(dgInProgress[myCell]);
//open form showing the request
// Create a new instance of the child form.
frmShowReq childForm = new frmShowReq();
// Make it a child of this MDI form before showing it.
childForm.MdiParent = this.MdiParent;
childForm.Text = "IO Codes";
childForm.Show();
}
}
private void dgComplete_CurrentCellChanged(object sender, EventArgs e)
{
if (this.dgComplete.CurrentRowIndex.ToString() == "-1")
{
MessageBox.Show("First select a print request", "No request selected.", MessageBoxButtons.OK);
}
else
{
//get collectionid number
DataGridCell myCell = new DataGridCell();
myCell.RowNumber = dgComplete.CurrentRowIndex;
myCell.ColumnNumber = 4;
Main.Req.strReq = Convert.ToString(dgComplete[myCell]);
//open form showing the request
// Create a new instance of the child form.
frmShowReq childForm = new frmShowReq();
// Make it a child of this MDI form before showing it.
childForm.MdiParent = this.MdiParent;
childForm.Text = "IO Codes";
childForm.Show();
}
}
private void dgNew_CurrentCellChanged(object sender, EventArgs e)
{
if (this.dgNew.CurrentRowIndex.ToString() == "-1")
{
MessageBox.Show("First select a print request", "No request selected.", MessageBoxButtons.OK);
}
else
{
//get collectionid number
DataGridCell myCell = new DataGridCell();
myCell.RowNumber = dgNew.CurrentRowIndex;
myCell.ColumnNumber = 4;
Main.Req.strReq = Convert.ToString(dgNew[myCell]);
//open form showing the request
// Create a new instance of the child form.
frmShowReq childForm = new frmShowReq();
// Make it a child of this MDI form before showing it.
childForm.MdiParent = this.MdiParent;
childForm.Text = "IO Codes";
childForm.Show();
}
}
private void btnRefresh_Click(object sender, EventArgs e)
{
dsNew1.Clear();
dsComplete1.Clear();
inProgress1.Clear();
sqlDataAdapter1.Fill(dsNew1, "MyImages");
sqlDataAdapter2.Fill(inProgress1, "MyImages");
sqlDataAdapter3.Fill(dsComplete1, "MyImages");
}
}
}
Last edited by Beast777; Aug 2nd, 2007 at 08:10 AM.
-
Aug 2nd, 2007, 08:11 AM
#12
Thread Starter
Fanatic Member
Re: Refreshing Parents datagrid
Form2
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ELibrary
{
public partial class frmShowReq : Form
{
SqlConnection myConnection = new SqlConnection();
SqlCommand myCommand;
SqlDataReader myDataReader;
SqlConnection myConnection2 = new SqlConnection();
SqlCommand myCommand2;
SqlDataReader myDataReader2;
string strTitle;
string strDesc;
string id;
private DataRow data;
public frmShowReq()
{
InitializeComponent();
}
private void frmShowReq_Load(object sender, EventArgs e)
{
//Change form text to show req number
this.Text= "Print Request Number " + Main.Req.strReq;
//Populate the Listview with the request
listView1.Columns.Add("ID", 40, HorizontalAlignment.Left);
listView1.Columns.Add("Title", 140, HorizontalAlignment.Left);
listView1.Columns.Add("Description", 140, HorizontalAlignment.Left);
listView1.Columns.Add("Status", 140, HorizontalAlignment.Left);
myConnection.ConnectionString = "blah blah";
myConnection.Open();
string cmdtext;
cmdtext = "Select PrintID, DocID, Status from PrintJobs where CollectionID like '" + Main.Req.strReq + "'";
myCommand = new SqlCommand(cmdtext, myConnection);
myDataReader = myCommand.ExecuteReader();
while (myDataReader.Read())
{
ListViewItem ls;
ls = new ListViewItem(myDataReader.GetValue(0).ToString());
//Get Title, Desc from other table
myConnection2.ConnectionString = "blah blah";
myConnection2.Open();
string cmdtext2;
cmdtext2 = "Select Title, Description from MyImages where DocID like '" + myDataReader.GetValue(1) + "'";
myCommand2 = new SqlCommand(cmdtext2, myConnection2);
myDataReader2 = myCommand2.ExecuteReader();
while (myDataReader2.Read())
{
strTitle = myDataReader2.GetString(0);
strDesc = myDataReader2.GetString(1);
}
myDataReader2.Close();
myConnection2.Close();
ls.SubItems.Add(strTitle);
ls.SubItems.Add(strDesc);
ls.SubItems.Add(myDataReader.GetValue(2).ToString());
listView1.Items.Add(ls);
}
myDataReader.Close();
myConnection.Close();
//Populate Binding Combo
myConnection.ConnectionString = "blah blah";
myConnection.Open();
cmdtext = "Select Type from Binding order by Type ASC";
myCommand = new SqlCommand(cmdtext, myConnection);
myDataReader = myCommand.ExecuteReader();
while (myDataReader.Read())
{
cboMethod.Items.Add(myDataReader.GetValue(0));
}
myDataReader.Close();
myConnection.Close();
//Populate IO Combo
myConnection.ConnectionString = "blah blah";
myConnection.Open();
cmdtext = "Select * from IOListing order by IONumber ASC";
myCommand = new SqlCommand(cmdtext, myConnection);
myDataReader = myCommand.ExecuteReader();
while (myDataReader.Read())
{
cboIO.Items.Add(myDataReader.GetValue(4));
}
myDataReader.Close();
myConnection.Close();
//Populate Cover Color Combo
myConnection.ConnectionString = "blah blah";
myConnection.Open();
cmdtext = "Select Color from CoverColor order by Color ASC";
myCommand = new SqlCommand(cmdtext, myConnection);
myDataReader = myCommand.ExecuteReader();
while (myDataReader.Read())
{
cboCoverColor.Items.Add(myDataReader.GetValue(0));
}
myDataReader.Close();
myConnection.Close();
//populate number of copies
int x = 1;
do
{
cboNumber.Items.Add(x);
x++;
} while (x < 1001);
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
myConnection.ConnectionString = "blah blah";
myConnection.Open();
ListView.SelectedIndexCollection indexes = this.listView1.SelectedIndices;
foreach (int index in indexes)
{
id = this.listView1.Items[index].SubItems[0].Text;
}
String cmdtext = "Select * from PrintJobs where PrintID like '" + id + "'";
myCommand = new SqlCommand(cmdtext, myConnection);
myDataReader = myCommand.ExecuteReader();
while (myDataReader.Read())
{
myConnection2.ConnectionString = "blah blah";
myConnection2.Open();
string cmdtext2;
cmdtext2 = "Select Title, Description from MyImages where DocID like '" + myDataReader.GetValue(2) + "'";
txtDocID.Text = id;
myCommand2 = new SqlCommand(cmdtext2, myConnection2);
myDataReader2 = myCommand2.ExecuteReader();
while (myDataReader2.Read())
{
txtTitle.Text = myDataReader2.GetString(0);
txtDesc.Text = myDataReader2.GetString(1);
}
myDataReader2.Close();
myConnection2.Close();
//txtTitle.Text = this.listView1.Items[index].SubItems[1].Text;
//txtDesc.Text = this.listView1.Items[index].SubItems[2].Text;
cboNumber.Text = Convert.ToString(myDataReader.GetValue(3));
cboMethod.Text = Convert.ToString(myDataReader.GetValue(4));
cboCoverColor.Text = Convert.ToString(myDataReader.GetValue(6));
dtDue.Text = Convert.ToString(myDataReader.GetValue(5));
cboColor.Text = Convert.ToString(myDataReader.GetValue(7));
cboIO.Text = Convert.ToString(myDataReader.GetValue(12));
txtInstructions.Text = Convert.ToString(myDataReader.GetValue(8));
txtName.Text = Convert.ToString(myDataReader.GetValue(9));
txtPhone.Text = Convert.ToString(myDataReader.GetValue(10));
cboStatus.Text = Convert.ToString(myDataReader.GetValue(14));
if (Convert.ToString(myDataReader.GetValue(11)) == "True")
{
chkContact.Checked = true;
}
else
{
chkContact.Checked = false;
}
}
myDataReader.Close();
myConnection.Close();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
//CHeck to see if there are any items
if (txtDocID.Text.Length < 0)
{
MessageBox.Show("There is no print job selected to update.", "Error!");
}
else
{
try
{
//Update the record to Approved
myConnection = new SqlConnection("blah blah");
string cmdtext = "Update PrintJobs SET Number = @Number, Method = @Method, DueDate = @DueDate, CoverColor = @CoverColor, Color = @Color, Instructions = @Instructions, Name = @Name, Phone = @Phone, Contact = @Contact, IO = @IO, Status = @Status where PrintID = '" + id + "'";
SqlCommand sqlcmd;
sqlcmd = new SqlCommand(cmdtext, myConnection);
sqlcmd.Parameters.AddWithValue("@Number", cboNumber.Text);
sqlcmd.Parameters.AddWithValue("@Method", cboMethod.Text);
sqlcmd.Parameters.AddWithValue("@DueDate", dtDue.Text);
sqlcmd.Parameters.AddWithValue("@CoverColor", cboCoverColor.Text);
sqlcmd.Parameters.AddWithValue("@Color", cboColor.Text);
sqlcmd.Parameters.AddWithValue("@Instructions", txtInstructions.Text);
sqlcmd.Parameters.AddWithValue("@Name", txtName.Text);
sqlcmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
sqlcmd.Parameters.AddWithValue("@Contact", chkContact.Checked.ToString());
sqlcmd.Parameters.AddWithValue("@IO", cboIO.Text);
sqlcmd.Parameters.AddWithValue("@Status", cboStatus.Text);
myConnection.Open();
sqlcmd.ExecuteNonQuery();
MessageBox.Show("Print job has been successfully changed.", "Print job changed", MessageBoxButtons.OK);
myConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error!");
myConnection.Close();
}
}
}
}
}
-
Aug 2nd, 2007, 07:24 PM
#13
Re: Refreshing Parents datagrid
The BindingSource.Current property returns the item that is currently selected in any controls bound to that BindingSource. If the data source is a DataTable then the Currnet property returns a DataRowView. It returns it via an Object reference though, because it can any type of object, thus you need to cast the property value as type DataRowView. In VB.NET you use DirectCast to perform a cast. How do you cast in C#? Once you have the bound DataRowView you simply get the associated DataRow from its Row property. This
vb.net Code:
Dim row As DataRow = DirectCast(Me.BindingSource1.Current, DataRowView).Row
becomes this:
C# Code:
DataRow row = ((DataRowView)this.BindingSource1.Current).Row;
When you consider the PURPOSE of the code it's fairly self-evident.
What do you mean you've never used Dialogue? A dialogue is just a form, displayed by calling its ShowDialog method. Again, create a form, pass it the DataRow, display it as a dialogue, load the values from the DataRow, then if and when the user presses OK you save the new values back to the DataRow. That's it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|