Hello,
I want to ask, can anyone give me a little tutorial about how to using CurrencyManager.. From what I know CurrencyManager can be use as a pointer to a record in dataset. So my Problem is:
I have 2 Form. Form1 have a datagrid that binding with dataset that I've retrieve from server, if i select any record on datagrid and click button 'view', I want to show form2 that have a textbox to be filled with record that I've selected before and if I change a value in textbox and click button 'save' form2 will be closed and the record will be updated to a server, form1 datagrid record will be updated too.
I'm still new to C# language :D Hope my question is clear enough. sorry for my bad english. :P
Loading
Andy WijPosted Sep 26, 2011, 8:16 AM
Form1 have a datagrid and button Edit, Form2 have a detail of selected record from Form1 in textbox.
So my problem is when I edit record in form2 and save it, datatable have change but not the database in server. am I missing something?
this is my code:
Form1
------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using KK.Class;
namespace KK
{
namespace Master.Forms
{
public partial class Currency : Form
{
public SqlDataAdapter da = null;
public DataTable dt = null;
//public BindingSource bs = null;
//public DataSet ds;
private string sql = null;
//public DataViewManager dv;
public CurrencyManager myCurrencyManager = null;
public SqlCommandBuilder cb;
public Currency()
{
InitializeComponent();
}
private void Currency_Load(object sender, EventArgs e)
{
Connection con = new Connection();
sql = "select * from currency";
da = new SqlDataAdapter(sql, con.open_connection());
cb = new SqlCommandBuilder(da);
//ds = new DataSet();
dt = new DataTable();
//da.TableMappings.Add("Table", "Currency");
da.Fill(dt);
//bs = new BindingSource();
//bs.DataSource = dt;
grid_currency.DataSource = dt;
//da.Fill(ds);
//bs = new BindingSource();
//bs.DataSource = ds.Tables["Currency"];
//da.Fill(ds);
//grid_currency.DataSource = bs;
//dv = ds.DefaultViewManager;
txt_tes.DataBindings.Add("Text", dt, "Name");
myCurrencyManager = (CurrencyManager)this.BindingContext[dt];
//myCurrencyManager.Position = 0;
}
private void cmd_edt_Click(object sender, EventArgs e)
{
int position = Convert.ToInt32(myCurrencyManager.Position.ToString());
Currency_det currency_det = new Currency_det(da, dt, position);
currency_det.BindingContext = this.BindingContext;
if (currency_det.ShowDialog() == DialogResult.OK)
{
dt.AcceptChanges();
grid_currency.Refresh();
}
}
private void cmd_del_Click(object sender, EventArgs e)
{
try
{
grid_currency.Rows.RemoveAt(grid_currency.CurrentRow.Index);
da.Update(dt);
MessageBox.Show("Data success updated");
}
catch
{
MessageBox.Show("Data fail updated");
}
}
private void cmd_ext_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
}
Form2
------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace KK
{
namespace Master.Forms
{
public partial class Currency_det : Form
{
public DataTable dtt;
public SqlDataAdapter dat;
public CurrencyManager myCurrencyManager;
int position;
public SqlCommandBuilder cb;
public Currency_det(SqlDataAdapter dAdapter ,DataTable dTable,int pos)
{
position = pos;
dtt = dTable;
dat = dAdapter;
InitializeComponent();
}
private void Currency_det_Load(object sender, EventArgs e)
{
cb = new SqlCommandBuilder(dat);
myCurrencyManager = (CurrencyManager)this.BindingContext[dtt];
myCurrencyManager.Position = position;
txt_name.DataBindings.Add("Text", dtt , "Name");
txt_symbol.DataBindings.Add("Text", dtt, "Symbol");
txt_rate.DataBindings.Add("Text", dtt, "Rate");
txt_nation.DataBindings.Add("Text", dtt, "Country");
}
private void cmd_ext_Click(object sender, EventArgs e)
{
this.Close();
}
private void cmd_save_Click(object sender, EventArgs e)
{
dat.Update(dtt);
this.DialogResult = DialogResult.OK;
}
}
}
}
If I edit record directly from datagrid and call update method, It can successful update to grid and database in server..
Mayur DighePosted Sep 13, 2011, 12:19 PM
please refer following articles...
http://msdn.microsoft.com/en-us/library/system.windows.forms.currencymanager
http://www.akadia.com/services/dotnet_databinding.html
http://www.freeweb.hu/noiseehc/CurrencyManager.html
http://msdn.microsoft.com/en-us/library/system.windows.forms.currencymanager
http://www.codeguru.com/forum/showthread.php?t=475727
http://stackoverflow.com/questions/5302482/update-currencymanager-position
To update DataGrid's Data you use DataSet's UpdateCommand()...
http://msdn.microsoft.com/en-us/library/33y2221y
http://www.homeandlearn.co.uk/net/nets12p9.html