开发者

How can I create a new DataGridView control for each new row in another datagridview in vb.net?

I have a DataGridView control (DataGridView6) that displays a list of managers. I want to gene开发者_Python百科rate a new DataGridView everytime I add a new manager to the list and put it in a specific place on my form.

EDIT:

say if i have a main datagridview, and i want to add another datagridview of the same size directly below it, how would i achieve this using the event handler method described in your answer below? im not sure if this is the most efficient way of displaying new members in the program though...

How do can I do this as simply as possible?


Use the DataGridView's "RowsAdded" event. Every time you add a new row (ie manager) to DataGridView6, have the event handler create a new DataGridView and place it where you want it.

It's hard to give a more detailed answer without the specifics of your implementation, but something like that should work.

EDIT - So something like this?

DataGridView dgv = new DataGridView();
dgv.Location = new Point(DataGridView6.Location.X,DataGridView6.Location.Y + <somevalue>);

If you need to keep adding them below this, you could just make a variable NextY that you increment each time you add a new one. You can store them all in a LinkedList or something similar so you can access them easily in order.


I'm not very good at VB, so I've written it in C# first:

DataGridView DataGridView6;
DataGridView DataGridView7;

DataGridViewRow CreateRow(object data) {
  DataGridViewRow row = null;
  int index = DataGridView6.Rows.Add();
  row = DataGridView6.Rows[index];
  // row.Cells[0] = something;
  // basically, add your date
  return row;
}

void DisplayManagerRow(DataGridViewRow row) {
  DataGridView7.DataSource = null;
  int columns = (DataGridView6.Columns != null) ? DataGridView6.Columns.Count : 0;
  if ((row != null) && (0 < columns)) {
    DataGridView7.Columns.Clear();
    List<DataGridViewColumn> cols = new List<DataGridViewColumn>(columns);
    for (int i = 0; i < columns; i++) {
      DataGridViewColumn dgvCol = (DataGridViewColumn)DataGridView6.Columns[i].Clone();
      DataGridView7.Columns.Add(dgvCol);
    }
    DataGridView7.Rows.Add(row);
  }
}

Now, to try this in VB:

private DataGridView6 As DataGridView
private DataGridView7 As DataGridView

Private Function CreateRow(ByVal data As Object) As DataGridViewRow
  Dim index As Int16 = DataGridView6.Rows.Add()
  Dim row As DataGridViewRow = DataGridView6.Rows(index)
  ' row.Cells(0) = something
  ' basically, add your date
  Return row
End Function

Private Sub DisplayManagerRow(ByVal row As DataGridViewRow)
  DataGridView7.DataSource = Nothing
  Dim columns As Int32 = 0
  If Not (DataGridView6.Columns = Nothing) Then
    columns = DataGridView6.Columns.Count
  End If
  If ((row Is Not Nothing) And (0 < columns)) Then
    DataGridView7.Columns.Clear()
    Dim cols As List<DataGridViewColumn> = new List<DataGridViewColumn>(columns)
    For (Dim i As Int32 = 0; i < columns; i++) 
      Dim dgvCol As DataGridViewColumn = CType(DataGridView6.Columns(i).Clone(), DataGridViewColumn)
      DataGridView7.Columns.Add(dgvCol)
    Next For
    DataGridView7.Rows.Add(row)
  End If
End Sub

I can't even remember how to write a For loop in VB! Pathetic!

Does that get the point across, though?

Is this what you are trying to do?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜