2014-10-06 3 views
1

DataGridView의 일부 열이 데이터베이스에서로드됩니다. 또한 DataGridView에 일부 열을 수동으로 추가했습니다. "S_No"라는 첫 번째 열의 모든 셀에 대해 값을 증가 시키려고합니다. DataGridview 셀에 대한 자동 증가 방법

나는 코드가 내 문제를 명확히 희망 :

코드에서
private void bindgrid() 
{ 
    dataGridView1.ColumnCount = 12; 
    ConnectionStringSettings consettings = ConfigurationManager.ConnectionStrings["attendancemanagement"]; 
    string connectionString = consettings.ConnectionString; 
    SqlConnection cn = new SqlConnection(connectionString); 
    cn.Open(); 

    string dtp = dateTimePicker3grd.Value.ToString("dd/MM/yyyy"); 
    string query = "select s_no,Employee_id,Employee_name from Employee_Details where employee_id not in (select employee_id from dailyattendance where date = '" + dtp + "') order by S_No "; 

    SqlCommand cmd = new SqlCommand(query, cn); 
    using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) 
    { 
     using (DataTable dt = new DataTable()) 
     { 
      sda.Fill(dt); 

      dataGridView1.Columns[0].Name = "S_No"; 
      dataGridView1.Columns[0].HeaderText = "S_No";//*Here I Want to increment cell value by 1 for every cell..and the first cell of the column should be the last serial value of database* 
      dataGridView1.Columns[1].HeaderText = "Employee_id"; 
      dataGridView1.Columns[1].Name = "Employee_Id"; 
      dataGridView1.Columns[1].DataPropertyName = "Employee_id"; 

      dataGridView1.Columns[2].Name = "Employee_name"; 
      dataGridView1.Columns[2].HeaderText = "Employee_Name"; 
      dataGridView1.Columns[2].DataPropertyName = "Employee_name"; 

      dataGridView1.Columns[3].Name = "In_time"; 
      dataGridView1.Columns[3].HeaderText = "In_time"; 

      dataGridView1.Columns[4].Name = "Out_time"; 
      dataGridView1.Columns[4].HeaderText = "Out_time"; 

      dataGridView1.Columns[5].Name = "Date"; 
      dataGridView1.Columns[5].HeaderText = "Date"; 

      dataGridView1.Columns[6].Name = "Week_no_of_the_Month"; 
      dataGridView1.Columns[6].HeaderText = "Week_no_of_the_Month"; 


      dataGridView1.Columns[7].HeaderText = "Attendance"; 
      dataGridView1.Columns[7].Name = "Attendance"; 

      dataGridView1.Columns[8].HeaderText = "Image_of_the_Employee"; 
      dataGridView1.Columns[8].Name = "Image_of_the_Employee"; 

      dataGridView1.Columns[9].Name = "Image_path"; 
      dataGridView1.Columns[9].HeaderText = "Image_path"; 

      dataGridView1.Columns[10].Name = "Work_status"; 
      dataGridView1.Columns[10].HeaderText = "Work_status"; 

      dataGridView1.Columns[11].Name = "Remarks"; 
      dataGridView1.Columns[11].HeaderText = "Remarks"; 
      dataGridView1.DataSource = dt; 

     } 
    } 
} 
+0

당신이 그 'DataTable'가 아닌'DataGridView'의 :

int maxSlNo = int.Parse(dt.Rows[dt.Rows.Count - 1]["s_no"].ToString()); maxSlNo++; foreach (DataRow dtRow in dt.Rows) { dtRow["s_no"] = maxSlNo; maxSlNo++; } dt.AcceptChanges(); 

또한, 다음 줄을 추가합니다. 데이타베이스에서 그것을 구성하면,'DataTable'은 자동적으로 동일한 속성을 취하게 될 것입니다. 그러나 당신은 데이터 어댑터의'MissingSchemaAction' 속성을'AddWithKey'로 설정해야 할 수도 있습니다. ID 열이 SQL Server에서 ID로 설정 되었습니까? 그렇지 않으면 ID가 자동 증분되는 방식이므로 ID가 있어야합니다. – jmcilhinney

+0

그건 그렇고, 왜 그 방법에 로컬 변수를 사용하는 모든게 있습니까? 확실히 데이터를 저장하기 위해'DataTable'과 데이터 어댑터에 대한 참조를 유지해야합니다. – jmcilhinney

+0

제게 더 좋은 방법으로 도움이되는 코드를 업데이트하십시오. 학습자로서 datatable을 사용하는 방법을 모릅니다. 데이터 테이블 @jmcilhinney를 사용하여 코드를 편집하십시오. –

답변

1

라인 sda.Fill(dt); 후 다음 코드 줄을 추가합니다. 그게 효과가 있는지 확인하십시오. 나중에 쿼리의 레코드를 가져 왔는지 여부에 대한 예외 검사를 추가 할 수 있습니다.

dataGridView1.Columns[0].DataPropertyName = "s_no"; 
관련 문제