Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.8 kB
6
Indexable
Never
  public static void _LoadDataToDataGridView(string tableName, DataGridView dataGridView)
  {
      string selectQuery = $"SELECT * FROM {tableName};";
      try
      {
          using (SQLiteConnection conn = new SQLiteConnection(Form1.strdatabase))
          {
              conn.Open();

              using (SQLiteDataAdapter adapter = new SQLiteDataAdapter())
              {
                  adapter.SelectCommand = new SQLiteCommand(selectQuery, conn);
                  DataTable dataTable = new DataTable();
                  adapter.Fill(dataTable);
                  if (!ColumnExists("Choose", dataGridView))
                  {
                      DataGridViewCheckBoxColumn chooseColumn = new DataGridViewCheckBoxColumn();
                      chooseColumn.DataPropertyName = "Choose";
                      chooseColumn.Name = "Choose";
                      dataGridView.Columns.Add(chooseColumn);
                  }

                  // Kiểm tra và thêm cột 'STT' nếu chưa tồn tại
                  if (!ColumnExists("STT", dataGridView))
                  {
                      DataGridViewTextBoxColumn textBoxColumn = new DataGridViewTextBoxColumn();
                      textBoxColumn.DataPropertyName = "STT";
                      textBoxColumn.Name = "STT";
                      dataGridView.Columns.Add(textBoxColumn);
                  }
                  dataGridView.DataSource = dataTable;
                  for (int i = 0; i < dataGridView.Rows.Count; i++)
                  {
                      dataGridView.Rows[i].Cells["STT"].Value = i+1;
                  }
              }

          }
          updateDtgv.WidthDtgv(dataGridView);
      }
      catch (Exception ex)
      {
        
         
      }
  }
Leave a Comment