Why can't I see the DataGridViewRow added to a DataGridView?

前端 未结 3 767
温柔的废话
温柔的废话 2021-01-13 02:02

I am trying to show rows in a DataGridView.

Here\'s the code:

foreach (Customers cust in custList)
            {
                string[] rowValues =         


        
相关标签:
3条回答
  • 2021-01-13 02:18
    List<customer> custList = GetAllCustomers();
                dataGridView1.Rows.Clear();
    
                foreach (Customer cust in custList)
                {
                    //First add the row, cos this is how it works! Dont know why!
                    DataGridViewRow R = dataGridView1.Rows[dataGridView1.Rows.Add()];
                    //Then edit it
                    R.Cells["Name"].Value = cust.Name;
                    R.Cells["Address"].Value = cust.Address;
                    R.Cells["Phone"].Value = cust.PhoneNo;
                    //Customer Id is invisible but still usable, like,
                    //when double clicked to show full details
                    R.Tag = cust.IntCustomerId;
                }
    

    http://aspdiary.blogspot.com/2011/04/adding-new-row-to-datagridview.html

    0 讨论(0)
  • 2021-01-13 02:23

    I found this was not my solution as I did not want to clear all the rows in the datagridview to simply re-insert them with one extra\new row. My collections are for tens of thousands, and the above solution was way way to slow....

    After more playing with the DataGridViewRow class i found a method for NewRow.CreateCells(DataGridView, object[]).

    This allows the cells to be drawn properly with the formats from the datagridview.

    It seems the question code at the top of this page left the cells blank because the types did not match when the new row was being inserted into the data gridview. Using the CreateCells method on the new row and parsing the DataGridView which the row will be inserted to, allows the cell types to be passed into the new row, which in turn, actualy draws the data on the screen.

    This then also allows you the place the tag on each item before the new row is added into the datagridview which was a requirement for my task.

    Please see my code below as an example of this.....

        private void DisplayAgents()
        {
            dgvAgents.Rows.Clear();
            foreach (Classes.Agent Agent in Classes.Collections.Agents)
            {
                DisplayAgent(Agent, false);
            }
        }
    
        private void DisplayAgent(Classes.Agent Agent, bool Selected)
        {
            DataGridViewRow NewRow = new DataGridViewRow();
            NewRow.Tag = Agent;
            NewRow.CreateCells(dgvAgents, new string[]
            {
                 Agent.Firstname,
                 Agent.Surname,
                 Agent.Email,
                 Agent.Admin.ToString(),
                 Agent.Active.ToString(),
            });
    
            dgvAgents.Rows.Add(NewRow);
            NewRow.Selected = Selected;
        }
    
    0 讨论(0)
  • 2021-01-13 02:34
    foreach (Customers cust in custList)
                {
                      DataGridViewRow row = new DataGridViewRow();
                      dataGridView1.BeginEdit();
                      row.Cells["Name"] = cust.Name;
                      row.Cells["Phone"] = cust.PhoneNo;
                      row.Tag = cust.CustomerId;
                      dataGridView1.Rows.Add(row);
                      dataGridView1.EndEdit();
                }
    

    try this

    0 讨论(0)
提交回复
热议问题