Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I know only two ways to do this: 1 by using oledbcommand or sqlcommand with a command string of "Insert into ..." and the other by inputting directly into a datagridview and update the database using dataadapter. Could you please share with me more ways to input data?(such as by using databinding?).
Thank you so much!
I prefer seeing example codes to anything!
Posted
Updated 11-Apr-11 13:57pm
v2

databinding does not insert data into database, using sqlcommands and dataAdapters i think are the two ways you can insert into databases.
 
Share this answer
 
You should read more about ADO.Net basics.
Then you should learn about LINQ and if you still have desire to be a coder you should learn about Entity framework.

And try to look for answers yourself, this topic isn't esoteric ;)! http://www.youtube.com/watch?v=4kBXLv4h2ig&feature=related

When it comes to ADO.Net every action related to transfer of data to or from database needs two things:
open connection and DBCommand object(SqlCommand, OledbCommand, Odbc... they all inherit from DBCommand class).
DataAdapter is "middle man" between datatables and commands.

Data binding is nothing more than connecting values on controls with data within particular DataRow, after you are done with changes those updates can be sent to DB or scrapped.

First of all you need data set (preferably with adapters).
You can bind data to the consumer form in three ways:
1. From Data Sources window, you simply choose table from your data set and drag and drop column representations to your form. This way everything is done by IDE for you.
2. You add binding source component to consumer form from Tools window and connecting it to the data set or data table within data set. Then you bind particular controls on your form to values from binding source.
3. Do all by hand. I wouldn't go there :).


Addendum:
I recommend this recipe:
DoubleClick for editing selected row and explicit Add new row call (via button or something).
This is simplified version.

First you must set your dataGridView
//this can be done in designer or by hand
myDataGridView.DataSource = myDataSet;
myDataGridView.DataMember = "TableName";
myDataGridView.AllowUserToAddRows = false;
//dataGridView customization (Columns, styles...)comes here

myDataGridView.DoubleClick += new EventHandler(myDataGridView_DoubleClick);


Event handler for double click can be something like this:

void myDataGridView_DoubleClick(object sender, EventArgs e)
{
    if(myDataGridView.SelectedRows.Count > 0)
    {
        int rowID = (int)myDataGridView.SelectedRows[0].Cells["IDColumn"].Value;
        MyDataInputForm frm = new MyDataInputForm();
        frm.Data = myDataSet;
        frm.SetEdit(rowID);
        if(frm.ShowDialog() == DialogResult.OK)
        {
            //validation?
            frm.EndEdit();
        }
        else
        {
            frm.CancelEdit();
        }
    }
}


For adding new row you can call (from toolbar, button...) method like this
void AddNewRow()
{
    DatSet copy = myDatSet.Copy();

    MyDataInputForm frm = new MyDataInputForm();
    frm.Data = copy;
    DataRow dr = copy.Tables["TableName"].NewRow();
    //add initial values to row
    copy.Tables["TableName"].Rows.Add(dr);
    frm.SetEdit((int)dr["IDColumn"]);
    if(frm.ShowDialog() == DialogResult.OK)
    {
        //validation?
        frm.EndEdit();
        myDatSet.Clear();
        myDatSet.Merge(copy);
    }
    else
    {
        frm.CancelEdit();
    }
}


MyDataInputForm inherits from IDataEditor<t> interface

public interface IDataEditor<t>
{
   void CancelEdit();

   void EndEdit();

   void SetEdit(T objectID);

   DataSet Data { get; set; }
}</t>



C#
//bindingSource, dataSet and dataBinding to controls is done in designer
public partial class MyDataInputForm : Form, IDataEditor<int>
{
    public MyDataInputForm ()
    {
        InitializeComponent();
    }        

    public void CancelEdit()
    {
        this.myBindingSource.CancelEdit();
    }

    public void EndEdit()
    {
        this.myBindingSource.EndEdit();
    }

    public void SetEdit(int objectID)
    {
        this.myBindingSource.Filter = "IDColumn=" + objectID;
        Application.DoEvents();
    }
        
    public DataSet Data
    {
        get
        {
            return this.dataSet;
        }
        set
        {
           this.dataSet= value;
           this.myBindingSource.DataSource = value;
           this.myBindingSource.DataMember = "TableName";
         }
    }
}</int>


You can change IDataEditor.Data to your custom typed dataSet.
Friendly advice to conclude this:
Don't rely to much on directly manipulating data with dataGridView unless you know what are you doing or you don't have big validating issues.

Cheers!
 
Share this answer
 
v2
Comments
[no name] 12-Apr-11 5:34am    
Maybe my original question doesn't show clearly what I mean.
I know using dataadapter is a standard way to insert into or update anything to the database.
But before using datadapter to do that, we should have a datatable or datasource (may be bound to a datagridview) first. And what I want is how to change data (update, insert, delete) in the datatable or dataset easily and properly for a huge database (more than 50 tables) except inputing data directly into the datagridview? Yeah that's why I have thought to "DataBindings". I have used a databindings like this txtbox1.DataBindings.Add("Text", tablename, "columnname"); But it seems to only help me show the value of the column at the current record to the textbox (reading), but I also want the value input into the textbox to be pushed to the column at a specified record (writing) whenever I want (such as when Enter pressed).
That's it! Now could you please give me a solution for that?
Oshtri Deka 12-Apr-11 8:00am    
Use Filter or Position property of BindingSource.
At the moment I can't write full solution. If no one answers, I'll write it this evening.
[no name] 12-Apr-11 9:22am    
Thank you! Hope you understand what I want! I want to use DataBindings to synchronize data between a datatable, dataset (or datagridview if possible) and a form with many controls used to both input and display data, that means I can edit data right in datagridview or through form. I don't want to use the normal ways (get data from the form first and put data to the datagridview as well as datatable or dataset and vice-versa).
Oshtri Deka 12-Apr-11 14:30pm    
If I finally understand, you want to use dataGridWiev control as a browser and form (dialog) to edit particular row. Correct?
Edit: I am sorry, I forgot. Please wait until morning.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900