I am using C# Windows For Application in .Net 2.0 and Visual Studio 2005. I have 2 forms.
1. MDI form which has the datagridview.
2. Windows form used for entering the values.
There are 2 buttons on windows data entry form Their functionalities:
1. 'Enter' button: when clicked the data should be saved in the database. the data entry form should disappear. the datagridview on MDI form must be updated with the new entry.
2. 'Enter Another' button: when clicked the data should be saved in the database. the datagridview on MDI form must be updated with the new entry. the data entry form should be ready for another entry.
When i clicked on 'Enter Another' button, the datagridview on MDI form does not show updated data row unless I manually restart the form.
Another thing is that the implementation logic is clear to me but I am unable to convert it into C# code.
What could be solution?
Thanks in advance....
Loading
Rhishikesh SavadikarPosted May 13, 2010, 12:46 AM
http://social.msdn.microsoft.com/Forums/en/winforms/thread/fc995bd6-9479-4b80-a3e5-72bd6bf252a4
Adavesh ManagaonPosted May 11, 2010, 5:25 AM
private void button1_Click(object sender, EventArgs e)
{
EnterData();
this.Parent.FillGrid();
}
Rhishikesh SavadikarPosted May 11, 2010, 1:01 AM
Code on Data Entry form
public Form1()
{
InitializeComponent();
}
//button 'Enter Another' click event
private void button1_Click(object sender, EventArgs e)
{
EnterData();
MDIParent1 frm = new MDIParent1();
frm.FillGrid();
}
public void EnterData()
{
SqlConnection con = new SqlConnection("Data Source=;Initial Catalog=;User ID=;pwd=");
string sql = "INSERT INTO myTable (ColumnA, ColumnB, ColumnC) VALUES (" +
"'" + textBox1.Text + "' , '" + textBox2.Text + "' , '"
+ textBox3.Text + "')";
SqlCommand com = new SqlCommand(sql, con);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
Code on MDI Parent form:
public MDIParent1()
{
InitializeComponent();
}
private void ShowNewForm(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.Show();
}
public void FillGrid()
{
dataGridView1.DataSource = null;
SqlConnection con = new SqlConnection("Data Source=;Initial Catalog=;User ID=;pwd=");
string sql = "select * from myTable";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
private void MDIParent1_Load(object sender, EventArgs e)
{
FillGrid();
}
Now please let me know how can I update the datagridview on every entry made in the database. Thanks.....!!! Also please let me know whether I need to change any property of datagridview...
ajay rajuPosted May 10, 2010, 8:50 AM
i think you not give datasource address to datagidview in the 'Enter Another' button code, so it is not updated. write code like
datagridview1.datasource = ds.tables[0]; and also try write 'Enter' button code to 'Enter Another' button.
it may helps you, if it helps please mark as accept answer.
Thanks