简体   繁体   English

如何将动态控件添加到datagridview?

[英]How to add dynamic control to datagridview?

I am little bit confused here i want some code here for this one or control here is my requirement I have datagridview like this 我在这里有点困惑我想在这里为此一些代码或控制在这里是我的要求我有这样的datagridview 在此处输入图片说明

Now when i add 7 to total taka then another one will be shown like this one 现在,当我将7加到总塔卡时,将显示另一个 在此处输入图片说明

now real scenario when i add value 3 to total taka then in second gridview it should be shown like this 现在是真实场景,当我将值3添加到总塔卡中时,应在第二个gridview中显示如下

Srno  Meters
1     null
2     null
3     null

same should be repeated adding new rows to first datagridview how would i achieve this? 应该重复相同的操作,将新行添加到第一个datagridview中,我将如何实现呢?

You can try adding code to a CellEndEdit event handler, then you can show an already created hidden DataGridView as the second one or you can also create that DataGridView on the fly. 您可以尝试将代码添加到CellEndEdit事件处理程序中,然后可以将已创建的隐藏DataGridView为第二个,也可以动态创建该DataGridView It's up to you. 由你决定。 I prefer to show that DataGridView and initialize the number of rows. 我更喜欢显示DataGridView并初始化行数。 Here is the code helping you understand the idea: 以下代码可帮助您理解该想法:

//First you have to layout 2 DataGridViews at design time and set the Visible of the second 
//DataGridView to false
//Your dataGridView2 should also have 2 columns added at design time as shown
//in your second picture.
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e){
  //Suppose the column with header Total kaka has name TotalKaka
  if (dataGridView1.Columns[e.ColumnIndex].Name == "TotalKaka") {
      int i;
      if (int.TryParse(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString(), out i))
      {
          dataGridView2.Rows.Clear();
          dataGridView2.Rows.Add(i);
          for (int j = 0; j < i; j++)
             dataGridView2[0, j].Value = j + 1;
          dataGridView2.Show();
          dataGridView2.CurrentCell = dataGridView2[1, 0];
          dataGridView2.Focus();          
      }
  }
}
//you should have some Submit button to submit the values entered into the second
//dataGridView, we should process something and surely hide the dataGridView2
private void submit_Click(object sender, EventArgs e){
    dataGridView2.Hide();
    //other code to process your data
    //....
}

NOTE : This answers to your actual requirement in this question, I guess you may have more problems such as How to process the data entered in dataGridView2 ? 注意 :这回答了您对这个问题的实际要求,我想您可能还有其他问题,例如如何处理在dataGridView2中输入的数据 How to show the dataGridView2 in another form? 如何以另一种形式显示dataGridView2? ... such problems do exist and I think you should ask for the solution in other questions, don't try asking for solving them right in this question. ...确实存在此类问题,我认为您应该在其他问题中寻求解决方案,而不是尝试在此问题中寻求正确的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM