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

Recommended Answers

All 3 Replies

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

Tekkno

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.