2011-05-22 27 views
6

我有一個datagridview。我將它綁定到一個列表。現在我想在結尾處顯示一列。但該專欄卻錯誤地陳述了。在數據綁定中添加按鈕列datagridview

這是我的代碼

grdPatientAppointment.DataSource = lst; 


     grdPatientAppointment.Columns["ID"].Visible = false; 
     //grdPatientAppointment.Columns["AdmitDate"].Visible = false; 
     //grdPatientAppointment.Columns["DischargeDate"].Visible = false; 
     grdPatientAppointment.Columns["AppointmentID"].Visible = false; 

     grdPatientAppointment.Columns["PatientrName"].DisplayIndex = 0; 
     grdPatientAppointment.Columns["Age"].DisplayIndex = 1; 
     grdPatientAppointment.Columns["Address"].DisplayIndex = 2; 
     grdPatientAppointment.Columns["ContactNo"].DisplayIndex = 3; 
     grdPatientAppointment.Columns["Dieseas"].DisplayIndex = 4; 
     grdPatientAppointment.Columns["AppointmentDate"].DisplayIndex = 5; 

     DataGridViewButtonColumn btnColumn = new DataGridViewButtonColumn(); 
     btnColumn.HeaderText = "Treat"; 
     btnColumn.Text = "Treat"; 
     btnColumn.UseColumnTextForButtonValue = true;    
     grdPatientAppointment.Columns.Insert(6,btnColumn); 

這裏是輸出:

here is output

,但我想,按鈕,數據網格視圖的最後

回答

4

添加列而不是將其插入GridView。它會自動將它追加到列集合的末尾。

grdPatientAppointment.Columns.Add(btnColumn); 
0

只需添加下面

grdPatientAppointment.Columns.Insert(I, btnColumn) 

我的代碼是列的索引要添加

0

使用grdPatientAppointment.AutoGenerateColumns = false;

然後添加的所有列網格將收到來自數據源和它們綁定來自編輯。

相關問題