2009-03-25 12 views
5

DataGridView'e bir veritabamı ekledim, onay kutularına sahiptim.Bir DatagridView üzerinden programlı olarak geçiş yapın ve onay kutularını işaretleyin

Datagridview'da gezinmek veya dolaşmak ve onay kutularını işaretlemek istiyorum, Aşağıda benim kullandığım sözdizimi var.

foreach(DataGridViewRow dr in dgvColumns.Rows) 
{ 
    DataGridViewCheckBoxCell checkCell = 
     (DataGridViewCheckBoxCell)dr.Cells["CheckBoxes"]; 
    checkCell.Value=1; 
    //Also tried checkCell.Selected=true; 
    //Nothing seems to have worked.! 
} 
+0

Veri kutusundaki alanlardan herhangi biriyle ilgili onay kutusu mu? Bunun yerine bir şablon alanı olabilir mi? Eğer işaretleme sonrası eğer daha temiz olabilir – Macros

+0

Hayır onay kutusu herhangi bir toher alan ile ilgili değil, ama bazı değerler dayalı bu veri değerleri –

cevap

2

bir DataTable bağlı ise, onun yerine modeli (tablo) üzerinde çalışabilirsiniz? DataGridView bir görünümdür ...

Değerleri ayarlama, tablodaki satırların üzerinden döngü yapmayı deneyin. Örneğin (aşağıda) - Ben DataGridView güncelleme olmadığını unutmayın - sadece DataTable:

foreach(DataGridViewRow dgvr in dgvColumns.Rows) 
{ 
    // Get the underlying datarow 
    DataRow dr = ((DataRowView)dgvr.DataBoundItem).Row; 

    // Update the appropriate column in the data row. 
    // Assuming this is your column name in your 
    // underlying data table 
    dr["CheckBoxes"] = 1; 
} 
+0

özür dilerim. Üzgünüm. model mi –

+0

Kodunuz için teşekkürler Marc, datagridview içindeki birleşik giriş kutusundan bir değer seçmemi sağlıyor mu? –

+0

Veriye bağlıysa, * bağlı * değerini değiştirmek bunu düzeltmelidir, inanıyorum. –

1

Something mükemmel :)

foreach (DataGridViewRow row in dgvDataGridView.Rows) 
{ 
    ((DataGridViewCheckBoxCell)row.Cells[0]).Value = true; 
} 
+0

((DataGridRowView) dgvr.DataBoundItem) .Row Satır te satır gibi bir özellik yok mu? –

+0

Üzgünüm, oyuncu yanlıştı. Yukarıdaki kodu değiştirdim. –

+0

Ayrıca datagridView'da bir comboBox var ve ayrıca bir değere ayarlamak istiyorum? Bu nasıl yapılır? –

11

benim için çalıştı, bu onay kutularını kontrol aşağıdakilerin: çizgisinde

using System; 
using System.Data; 
using System.Windows.Forms; 

static class Program 
{ 
    [STAThread] 
    static void Main() 
    { 
     DataTable table = new DataTable(); 
     table.Columns.Add("Name", typeof(string)); 
     table.Columns.Add("Selected", typeof(bool)); 
     table.Rows.Add("Fred", false); 
     table.Rows.Add("Jo", false); 
     table.Rows.Add("Andy", true); 

     Button btn = new Button(); 
     btn.Text = "Select all"; 
     btn.Dock = DockStyle.Bottom; 
     btn.Click += delegate 
     { 
      foreach (DataRow row in table.Rows) 
      { 
       row["Selected"] = true; 
      } 
     }; 

     DataGridView grid = new DataGridView(); 
     grid.Dock = DockStyle.Fill; 
     grid.DataSource = table; 

     Form form = new Form(); 
     form.Controls.Add(grid); 
     form.Controls.Add(btn); 
     Application.Run(form); 
    } 
} 
1

değerini seçilir satır böylece kaydedilmiş alamadım yatan veri kaynağı için iletilmez. veri kaynağı Datatable'dır. Datagridview problemi.

0
using System.Collections.Generic; 
using System.Windows.Forms; 

namespace FindTheCheckedBoxes 
{ 
    public partial class Form1 : Form 
    { 
     List<TestObject> list = new List<TestObject>(); 

     List<int> positionId = new List<int>(); 

     public Form1() 
     { 
      InitializeComponent(); 
      PopulateDataGrid(); 

      foreach (DataGridViewRow row in dataGridView1.Rows) 
      { 
       if ((bool)row.Cells[0].Value == true) 
        positionId.Add((int)row.Cells[1].Value); 
      } 

      // sets the window title to the columns found ... 
      this.Text = string.Join(", ", positionId); 
     } 
     void PopulateDataGrid() 
     { 
      list.Add(new TestObject { tick = false, LineNum = 1 }); 
      list.Add(new TestObject { tick = true, LineNum = 2 }); 
      list.Add(new TestObject { tick = false, LineNum = 3 }); 
      list.Add(new TestObject { tick = true, LineNum = 4 }); 

      dataGridView1.DataSource = list; 
     } 
    } 
    class TestObject 
    { 
     public bool tick { get; set; } 
     public int LineNum { get; set; } 
    } 
} 

Bu, ihtiyacınız olanı yapıyor gibi görünüyor. Yanlış cevap verdiysem, bu kadar özür dilerim. Sadece yardım etmeye çalışıyorum.

İlgili konular