如何使用DB从DataGridView中选择行到C#中的新DataGridView

时间:2014-12-05 15:37:24

标签: c# datagridview row

我有两个DataGridViews,

  1. datagridviewDoses来自DB的所有菜肴
  2. datagridviewDoseOrder为空
  3. 我想从datagridviewDoses按下按钮行。 该行将移至datagridviewDoseOrder。 我添加的按钮,我无法将行移动到-datagridviewDoseOrder。

      private void AddColumn()
    {
        DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
        buttonColumn.HeaderText = "בחר מנה";
        buttonColumn.Name = "בחר מנה";
        buttonColumn.Text = "בחר מנה";
        buttonColumn.UseColumnTextForButtonValue = true;
        dataGridViewDoses.Columns.Add(buttonColumn);
        dataGridViewDoses.CellClick += new DataGridViewCellEventHandler(dataGridViewDoses_CellClick);
    }
    void dataGridViewDoses_CellClick(object sender, DataGridViewCellEventArgs e)
    {
    
    }
    

1 个答案:

答案 0 :(得分:0)

我能够重新创建您的问题并按照以下方式解决:

示例类作为每个dgview的数据源。

public class Example
{
  public string Foo { get; set; }
  public string Bar { get; set; }
}

显示两个DataGridViews的表单。

public Form1()
{
  InitializeComponent();
  this.First = new BindingList<Example>()
  {
    new Example() { Foo = "foo1", Bar = "bar1" },
    new Example() { Foo = "foo2", Bar = "bar2" }
  };
  this.Second = new BindingList<Example>();

  this.dataGridView1.DataSource = this.First;
  this.dataGridView2.DataSource = this.Second;

  DataGridViewButtonColumn btnCol = new DataGridViewButtonColumn();
  btnCol.HeaderText = "Transfer";
  btnCol.Name = "Transfer";
  btnCol.Text = "Transfer";
  btnCol.UseColumnTextForButtonValue = true;
  this.dataGridView1.Columns.Add(btnCol);
  this.dataGridView1.CellClick += new DataGridViewCellEventHandler(col_Click);
}

public BindingList<Example> First { get; set; }
public BindingList<Example> Second { get; set; }

public void col_Click(object sender, DataGridViewCellEventArgs e)
{
  if (e.ColumnIndex == 0) // The button column somehow is column 0, Foo is 1, and Bar is 2.
  {
    this.Second.Clear();
    this.Second.Add(this.First[e.RowIndex]);
  }
}

通过单击按钮,该行的数据将移动到第二个DataGridView。我希望这会有所帮助。