Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi all,

actually topic tells the problem. details are;

i have a datagridview in windows forms which has data from an sql table. i've inserted checkboxes with DataGridViewCheckBoxColumn. I need to check one or multiple checkboxes(rows) to update selected rowS. there is a column which includes DateTime. I tried so many codes but all failed, i guess because of DateTime column.

i will be glad if you can give some examples as code.

Thanks in advance,
Posted
Updated 5-Oct-17 22:44pm

Sorry, I can only do this in VB:
VB
For each row as DataGridViewRow in Datagridview1.Rows
'Assuming first column has the checkboxes
   If row.Cells(0).Value = True Then

   'Carry out update and make sure your DateTime is properly formatted
   cmdUpdate = "UPDATE TABLE ... SET ... WHERE ..."

   End If
Next
 
Share this answer
 
Comments
tolgasenol 11-Jan-12 7:38am    
still i couldnt solve the issue.
Scubapro 11-Jan-12 8:09am    
You'll have to show some of where it goes wrong, otherwise it will be difficult to help you.
tolgasenol 11-Jan-12 9:51am    
Scubapro i pasted the code below. thank you very much!
yes scubapro you are absolutlely right. My fault sorry. Here is the code i try;
i added checkboxcolumn to the datagrid

but the problem is, only the uppest row updates. i guess i cannot return it!

C#
private void button1_Click(object sender, EventArgs e)
        {
            //sql bağlantı depo giriş
            SqlConnection sconn = new SqlConnection();
            sconn.ConnectionString = "Data Source=R2D2\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=depom";
            sconn.Open();
//
// here is where selection starts
//
            List<DataRow> lst = new List<DataRow>();


            for (int i = 0; i < malzemeDataGridView.Rows.Count; i++)
            {
                if (Convert.ToBoolean(malzemeDataGridView.Rows[i].Cells["sec"].Value) == true)
                {

                    SqlCommand ins = new SqlCommand();

                    ins.Connection = sconn;
                    ins.CommandText = "update malzeme set depo='Merkez', degistirmeT=@degT, konum=@knm where malzemeID=@malz";

                    ins.Parameters.AddWithValue("@malz", malzemeDataGridView.Rows[i].Cells["dataGridViewTextBoxColumn1"].Value.ToString());
                    ins.Parameters.AddWithValue("@degT", DateTime.Now.ToString());
                    ins.Parameters.AddWithValue("@knm", textBox1.Text = "");



                    //ons bağlantısı başlangıç
                    SqlCommand ons = new SqlCommand();

                    ons.Connection = sconn;
                    ons.CommandText = "insert into tarihegore (malzemeID, konum, depo, sinif, marka, model, aciklama, spectrumNO, seriNO, notlar, islemtarihi) values (@malzemeID, @konum, @depo, @sinif, @marka, @model, @aciklama, @spectrumNO, @seriNO, @notlar, @islemtarihi)";

                    ons.Parameters.AddWithValue("@malzemeID", malzemeIDTextBox.Text.Trim());
                    ons.Parameters.AddWithValue("@konum", konumTextBox.Text.Trim());
                    ons.Parameters.AddWithValue("@depo", depoTextBox.Text.Trim());
                    ons.Parameters.AddWithValue("@sinif", sinifTextBox.Text.Trim());
                    ons.Parameters.AddWithValue("@marka", markaTextBox.Text.Trim());
                    ons.Parameters.AddWithValue("@model", modelTextBox.Text.Trim());
                    ons.Parameters.AddWithValue("@aciklama", aciklamaTextBox.Text.Trim());
                    ons.Parameters.AddWithValue("@spectrumNO", spectrumNOTextBox.Text.Trim());
                    ons.Parameters.AddWithValue("@seriNO", seriNOTextBox.Text.Trim());
                    ons.Parameters.AddWithValue("@notlar", depoTextBox.Text.Trim() + " " + konumTextBox.Text.Trim() + " dan Merkeze geldi.");
                    ons.Parameters.AddWithValue("@islemtarihi", DateTime.Now.ToString());

                    //ons bağlantısı sonu

                    //sconn.Open();
                    MessageBox.Show(markaTextBox.Text.Trim() + " " + modelTextBox.Text.Trim() + " depoya girdi", "Depoya Giriş");



                    ins.ExecuteNonQuery();
                    ons.ExecuteNonQuery();




                    try
                    {
                        this.malzemeTableAdapter.Tümünü_Göster(this.dataSet1.malzeme);
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }

                    sconn.Close();
                }

            }
        }
 
Share this answer
 
C#
          for (int i = 0; i < malzemeDataGridView.Rows.Count; i++)
          {
              if (Convert.ToBoolean(malzemeDataGridView.Rows[i].Cells["the name of the combocolumn"].Value) == true)
              {
//your own code here...

              }


thanks all...
 
Share this answer
 

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