0

i connect mysql using c# and show database in datagridview
I want when i click the row in datagridview, textbox, combobox , datetimepicker will show form datagirdview
But mysql datetime : yyyy/mm/dd 00:00:00 ( 00:00:00 will default if don't write)
Datagridview : mm/dd/yyyy

i used code: datetimepicker1.DataBindings.Add("text", dataGridView1.DataSource, "Birthday");
and it appear errow. i don't know how to fix

3
Contributors
3
Replies
25
Views
2 Years
Discussion Span
Last Post by J.C. SolvoTerra
0

Hello Nguyen_4:
What is the error message that you're getting? Also can you list the code you've created?

Tekkno

0

Ok, Let's assume the following table:

Demo1.png

And the following code in your Form1 class:

        SqlConnection sqlCon;
        SqlCommand sqlCmd;
        String conString = "Server=SOLVOTERRA-HP;Database=Birthdays;
            Integrated Security=true";

        DataTable dTable;

        //DGV Cell Content Clicked Event
        private void dataGridView1_CellContentClick(object sender, 
            DataGridViewCellEventArgs e)
        {

            //Clear Previous Binding
            dateTimePicker1.DataBindings.Clear();

            //Reset New Binding
            dateTimePicker1.DataBindings.Add("text", 
                dataGridView1.DataSource, "Birthday");

        }


        public Form1()
        {
            InitializeComponent();

            //Create New Connection
            sqlCon = new SqlConnection(conString);

            //Create New Command
            sqlCmd = new SqlCommand("SELECT * FROM Person", sqlCon);

            //Create New Data Adapter
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCmd);

            dTable = new DataTable();
            dTable.TableName = "Results";

            //Fill The Table With Your Data
            sqlDataAdapter.Fill(dTable);

            //Close The Connection
            sqlCon.Close();

            //Set The DGV's Data Source To The Table
            dataGridView1.DataSource = dTable;

        }

Even though the date time stap formats don't match this wont raise an exception, in fact clicking on the row will update the DTP's value:

Demo2.png

As the DTV is bound to the DGV, updating the DTP's value will also update the DGV. Though this wont effect your database directly.

Demo3.png

Edited by J.C. SolvoTerra

0

Oh shucks... I just saw MySQL... Sorry. Ok, so you can ignore the data acquisition part, however the binding between your DGV and DTP will still be the same.

Edited by J.C. SolvoTerra

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.