2011-11-03 56 views
0

我有一个需求,就像我在List中有一个值列表。我需要在DataGridView中绑定一个Combobox并在Combobox中添加这些值。用datagridview中的Combobox绑定Ilist值

private void btnBrowse_Click(object sender, EventArgs e) 
    { 
      dgvFiles.Rows.Clear(); 
      dgvFiles.Columns.Clear(); 
      dgvFiles.Columns.Add("FileName", "File Name"); 
      dgvFiles.Columns.Add("Size", "File Size"); 
      dgvFiles.Columns.Add("Extension", "Extension"); 
      dgvFiles.Columns.Add("Non-OCR Pages", "Non-OCR Pages"); 
      dgvFiles.Columns.Add("OCR Pages", "OCR Pages");    
      dgvFiles.Columns.Add("Total Pages", "Total Pages"); 
      DataGridViewComboBoxColumn tb = new DataGridViewComboBoxColumn(); 
      dgvFiles.Columns.Add(tb); 
      tb.Name = "Non-OCR PageNos"; 
      tb.HeaderText = "Non-OCR PageNos"; 
      DataGridViewComboBoxColumn tb1 = new DataGridViewComboBoxColumn(); 
      dgvFiles.Columns.Add(tb1); 
      tb1.Name = "OCR PageNos"; 
      tb1.HeaderText = "OCR PageNos"; 
      string[] files = Directory.GetFiles(filepath, "*.pdf"); 
      //string[] templist = File.ReadAllLines("d:\\pdfcheck.txt"); 
      textBox1.Text = null; 
      textBox2.Text = null; 
      //StreamWriter OpStream = new StreamWriter("d:\\OCR_NonOCR_Report.csv"); 
      List<int> listNonOcr = new List<int>(); 
      List<int> listOcr = new List<int>(); 
      dgvFiles.Rows.Add(f.Name, f.Length, f.Extension, label4.Text.ToString(), label3.Text.ToString(),listNonOcr, listOcr, total.ToString(); 
    } 

回答

0

你可以像这样.....

public class SomeClass 
{ 
    private string name; 

    public SomeClass(string name) 
    { 
    this.name = name; 
    } 

    public string Name 
    { 
    get{return name;} 
    } 

    public object This 
    { 
    get{return this;} 
    } 
} 

so that you can specify: 

DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn(); 
column.ValueMember = "Name"; 
column.ValueMember = "This"; 
column.DataSource = new List<SomeClass>(new SomeClass[]{new 
SomeClass("Fred")});