Link to home
Start Free TrialLog in
Avatar of APPIREDDY
APPIREDDY

asked on

insert value into database from datagridview control

Hi experts,
i'm using mysql database and
select notes_date as Date,notes_message as Notes from client_notes where client_id=?cid query to show data in datgridview control.

now i want to insert data into client_notes tabele from the datagrid view control
as i'am  novoice to c# , can anybody tell me how this can be done in a best way programatically

Thanks
Avatar of Priest04
Priest04
Flag of Serbia image

If you want to use databinding mechanism provided by ADO.NET, bascally it goes like this:

1) you use DataAdapter to fill data to datatable

- create new datadapter, using theSELECT statemtent above mentioned-
MySqlDataAdapter da = new MySqlDataAdapter(SQLSelectString, myConnection);

- create new datatable
DataTable dt = new DataTable();

- use dataadapters Fill method to fill data to datatable
da.Fill(dt);

2) bind datagridview to this datatable
datahridview1.DataSource = dt;
3) after you have done editing datagridview, use dataadapters Update method to update data from tatatable to database
da.Update(dt);

Gpran
Avatar of APPIREDDY
APPIREDDY

ASKER

IF I DO LIKE THAT , I am getting runtime exception client_id doesn't have a default value.
because in my datagrid control I am only displaying two values and i'm taking client_id value from combobox
Well, you should have then explained better what you want to achieve. You can try inserting recors manually, something like this:

MySqlCommand cmd = new MySqlCommand ("INSERT INTO MyTable (field, field2, field3) VALUES (" + value1 + ", " + value2 + ", " + value3 + ")", myConnection);

myConnection.Open();
cmd.ExecuteNonQuery();
myConnection.Close();

the above example assumes all the fields are of numeric type, if you have string column, then you would need to put value between ' '.
CAN YOU HELP ME ,HO TO ACCESS THE CURRENT COLUM VALUE FROM DATAGRID CONTROL
No need for capital letters. :) There is a difference between datagridview and datagrid control, I gues you are talking about datagridview control, based on the title of the topic

// if you want to get string value of the first cell in first row
string temp = datagridview1[0, 0].Value.ToString();

// if you want to get integer value of the 2nd cell in fourth row
int temp = Convert.ToInt32(datagridview1[2, 4].Value);

Ah, forgot to add how to get the current cell value

dg.CurrentCell.Value

if you want to know what is the column/row index of the currently selected cell

dg.CurrentCell.ColumnIndex
dg.CurrentCell.RowIndex
Thanks Priest04,

I have placed datagridview control on the form.

What i want to do is, when the user clicks add button,the datagridview control should  insert an extra row and the cursor should be focused on that row,so that user can enter the row

currently the datagridviewcontrol is showing 1 extra empty row,which i dont want to show
can you tell me how can i achieve this
ASKER CERTIFIED SOLUTION
Avatar of Priest04
Priest04
Flag of Serbia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thankyou