LeeHenry
asked on
Update using DataGridView Control
I am trying to teach myself how to use the DataGridViewControl. What I would like to do is simply display data from a table of my MySql Database, and then click on any row item, edit the item, and update my mysql databse after the user presses enter or clicks save.
I have been able to display data from one of my MySql tables, but I can't seem to get a grasp on how I can update my database with the edited cell. It appears as this would be easier if I was using SqlServer, but i must use MySql. I posted my code that I used to load the DataGridViewControl.
private void InitializeDataGridView(Dat aSet ds)
{
try
{
for (int i = 0; i < ds.Tables.Count; i++)
{
DataRow newRow = ds.Tables[0].NewRow();
newRow.ItemArray = ds.Tables[i].Rows[0].ItemA rray;
ds.Tables[0].Rows.Add(newR ow);
}
dataGridView.DataSource = ds.Tables[0];
dataGridView.Refresh();
}
I have been able to display data from one of my MySql tables, but I can't seem to get a grasp on how I can update my database with the edited cell. It appears as this would be easier if I was using SqlServer, but i must use MySql. I posted my code that I used to load the DataGridViewControl.
private void InitializeDataGridView(Dat
{
try
{
for (int i = 0; i < ds.Tables.Count; i++)
{
DataRow newRow = ds.Tables[0].NewRow();
newRow.ItemArray = ds.Tables[i].Rows[0].ItemA
ds.Tables[0].Rows.Add(newR
}
dataGridView.DataSource = ds.Tables[0];
dataGridView.Refresh();
}
IDataAdapter FAdapter = new OleDbDataAdapter(UrConnect ion);
DataSet FInternalDataSet = new DataSet(); // replace with ur dataset
try
{
((OleDbDataAdapter)FAdapte r).SelectC ommand
= new OleDbCommand
("SELECT COMPANYCODE,USERNAME,ALERT CODE,OPERA TOR,VALUE"
+" FROM TR_ALERTDET WHERE 1=2");
((OleDbDataAdapter)FAdapte r).FillSch ema(FInter nalDataSet
,SchemaType.Mapped,"TR_ALE RTDET");
((OleDbDataAdapter)FAdapte r).SelectC ommand
= new OleDbCommand
("SELECT COMPANYCODE,USERNAME,ALERT CODE,OPERA TOR,VALUE"
+" FROM TR_ALERTDET WHERE USERNAME = @USERNAME");
((OleDbDataAdapter)FAdapte r).SelectC ommand.Par ameters.Ad d
("@USERNAME",null);
((OleDbDataAdapter)FAdapte r).DeleteC ommand
= new OleDbCommand
("DELETE FROM TR_ALERTDET WHERE USERNAME = @USERNAME");
((OleDbDataAdapter)FAdapte r).DeleteC ommand.Par ameters.Ad d
("@USERNAME",null);
((OleDbDataAdapter)FAdapte r).DeleteC ommand.Par ameters["@ USERNAME"]
.SourceColumn = "USERNAME";
((OleDbDataAdapter)FAdapte r).DeleteC ommand.
Parameters["@USERNAME"].So urceVersio n
= DataRowVersion.Current;
((OleDbDataAdapter)FAdapte r).InsertC ommand =
new OleDbCommand
("INSERT INTO TR_ALERTDET(COMPANYCODE,US ERNAME"+
",ALERTCODE,OPERATOR,VALUE ) VALUES(@COMPANYCODE,@USERN AME"+
",@ALERTCODE,@OPERATOR,@VA LUE)");
foreach(DataColumn Col in InternaDataTable.Columns)
{
((OleDbDataAdapter)FAdapte r).InsertC ommand.Par ameters.Ad d
("@"+Col.ColumnName,null);
((OleDbDataAdapter)FAdapte r).InsertC ommand.Par ameters["@ "+Col.Colu mnName]
.SourceColumn =Col.ColumnName;
((OleDbDataAdapter)FAdapte r).InsertC ommand.
Parameters["@"+Col.ColumnN ame].Sourc eVersion
= DataRowVersion.Current;
}
}
catch(Exception ex)
{
throw new Exception("Exception in prepare commands "+ex.Message);
}
i'm ising a table called tr_alertdet and where username is the primary key u can change the command for ur table respectively
while saving just call
((OleDbDataAdapter)FAdapte r).Update( FInternalD ataSet)
DataSet FInternalDataSet = new DataSet(); // replace with ur dataset
try
{
((OleDbDataAdapter)FAdapte
= new OleDbCommand
("SELECT COMPANYCODE,USERNAME,ALERT
+" FROM TR_ALERTDET WHERE 1=2");
((OleDbDataAdapter)FAdapte
,SchemaType.Mapped,"TR_ALE
((OleDbDataAdapter)FAdapte
= new OleDbCommand
("SELECT COMPANYCODE,USERNAME,ALERT
+" FROM TR_ALERTDET WHERE USERNAME = @USERNAME");
((OleDbDataAdapter)FAdapte
("@USERNAME",null);
((OleDbDataAdapter)FAdapte
= new OleDbCommand
("DELETE FROM TR_ALERTDET WHERE USERNAME = @USERNAME");
((OleDbDataAdapter)FAdapte
("@USERNAME",null);
((OleDbDataAdapter)FAdapte
.SourceColumn = "USERNAME";
((OleDbDataAdapter)FAdapte
Parameters["@USERNAME"].So
= DataRowVersion.Current;
((OleDbDataAdapter)FAdapte
new OleDbCommand
("INSERT INTO TR_ALERTDET(COMPANYCODE,US
",ALERTCODE,OPERATOR,VALUE
",@ALERTCODE,@OPERATOR,@VA
foreach(DataColumn Col in InternaDataTable.Columns)
{
((OleDbDataAdapter)FAdapte
("@"+Col.ColumnName,null);
((OleDbDataAdapter)FAdapte
.SourceColumn =Col.ColumnName;
((OleDbDataAdapter)FAdapte
Parameters["@"+Col.ColumnN
= DataRowVersion.Current;
}
}
catch(Exception ex)
{
throw new Exception("Exception in prepare commands "+ex.Message);
}
i'm ising a table called tr_alertdet and where username is the primary key u can change the command for ur table respectively
while saving just call
((OleDbDataAdapter)FAdapte
you must call the write command after wdit the data
in this sample
http://msdn2.microsoft.com/en-us/library/fbk67b6z.aspx
it is line
dataAdapter.Update((DataTa ble)bindin gSource1.D ataSource) ;
in this sample
http://msdn2.microsoft.com/en-us/library/fbk67b6z.aspx
it is line
dataAdapter.Update((DataTa
ASKER
Dave,
I saw this example on the mysql forums. I've already tried to get this to work. When I copy and paste the exact code, and use the country database like they mention, I can display the information, but nothing else is working. I set breakpoints on all the other functions (dataGridView_RowValidatin g etc...), and they are not reaching them. I didn't see anything in the properties of the dataGridView where I needed to specify these functions anywhere. I am probably missing something simple..??? any suggestions
AlexNex,
I think your example was for SqlServer.
gauthampj,
I'm going to take a look at yours later on tomm..
thanks for the tips
I saw this example on the mysql forums. I've already tried to get this to work. When I copy and paste the exact code, and use the country database like they mention, I can display the information, but nothing else is working. I set breakpoints on all the other functions (dataGridView_RowValidatin
AlexNex,
I think your example was for SqlServer.
gauthampj,
I'm going to take a look at yours later on tomm..
thanks for the tips
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
kbkbar7,
I'm not sure if i'm going to work on this again.. i've been busy with something else... If i find out i'm not going to get into this, i'll go ahead and give you the points since you were very helpfull.. I should be letting you know soon.. thanks again for the response
I'm not sure if i'm going to work on this again.. i've been busy with something else... If i find out i'm not going to get into this, i'll go ahead and give you the points since you were very helpfull.. I should be letting you know soon.. thanks again for the response
If not lem'me know and I'll throw together some code and we can step through it together...
Dave
http://forums.mysql.com/read.php?38,115063,115063