【问题标题】:How to add row to datagridview using list如何使用列表向datagridview添加行
【发布时间】:2017-06-26 11:58:01
【问题描述】:

我需要帮助使用列表向 datagridwiev 添加行。我有课

 public class GridObject
    {
        public string Worker
        {
            get;
            set;
        }

        [DisplayName("Item name")]
        public string ItemName
        { get; set; }

        public int Price { get; set; }

        public int Quantity { get; set; }


    }

我正在制作可以在 datagridview 中显示 slod 项目的 win 表单应用程序。到现在为止,我让它这样做了

private void btnCash_Click(object sender, EventArgs e)
        {
             dt = new System.Data.DataTable();
            using (SqlConnection con = Helper.ConnectionToDatabase)
            {                  
                SqlDataAdapter da = new SqlDataAdapter(string.Format(Resources.ViewCommand, tbUser.Text, ItemList.SelectedValue), con);
                da.Fill(dt);
                //dataDaily.Rows.Clear();
                foreach (DataRow item in dt.Rows)
                {
                    int n = dataDaily.Rows.Add();
                    dataDaily.Rows[n].Cells[0].Value = item[0].ToString();
                    dataDaily.Rows[n].Cells[1].Value = item[1].ToString();
                    dataDaily.Rows[n].Cells[2].Value = item[2].ToString();
                }
            }
        }

它有效,但我想添加一个不在数据库中的新列(数量),并且计算一个用户(在我的情况下为工人)出售的相同商品。我坚持使用列表购买我不知道如何填写列表,而不是在 datagridview 中显示它

【问题讨论】:

    标签: c# winforms list datagridview


    【解决方案1】:

    Win Forms 将 Datagridview 行添加到列表 将行从列表添加到 datagridview

    // 将行添加到列表

    List<DataGridViewRow> DeletedRowsList = new List<DataGridViewRow>(); // Row List
    
     // Add the row for deletion to the list
            private void AddRowToList()
            {
    
            if (Kunde_dataGridView.CurrentRow.Cells.Count == Kunde_dataGridView.ColumnCount) // If all cels are selected on the current row
            {
                // New Row
                DataGridViewRow row = (DataGridViewRow)Kunde_dataGridView.SelectedRows[0].Clone(); // Clone the row and assign it to the row that will be added to the list
                for (int i = 0; i < Kunde_dataGridView.SelectedCells.Count; i++)
                {
                    row.Cells[i].Value = Kunde_dataGridView.SelectedCells[i].Value;
                }
                DeletedRowsList.Add(row); // Add the Cloned Row to the list
            }
        }
    

    // 将行从列表插入到datagridview

        // Undo  Button
                private void undo_button_Click(object sender, EventArgs e)
                {
    
    
                if(DeletedRowsList.Count > 0)
                {
                    int lastindex = DeletedRowsList.Count - 1;
    

    //datagridview的DataSet可以在Form Load中找到

    advokathusetDataSet.Kunde.Rows.Add(DeletedRowsList[lastindex].Cells[0].Value, DeletedRowsList[lastindex].Cells[1].Value, DeletedRowsList[lastindex].Cells[2].Value, DeletedRowsList[lastindex].Cells[3].Value, DeletedRowsList[lastindex].Cells[4].Value, DeletedRowsList[lastindex].Cells[5].Value, DeletedRowsList[lastindex].Cells[6].Value);
                   SaveDatagridview(); // Save to DB  
                   DeletedRowsList.RemoveAt(lastindex); // Remove Last index
    
                }
    
    
            }
    

    // 仅将 Datagridview 行添加到列表中

    List<DataGridViewRow> DeletedRowsList = new List<DataGridViewRow>(); // Row List
     DataGridViewRow row = (DataGridViewRow)Kunde_dataGridView.SelectedRows[0].Clone(); // Clone the row and assign it to the row that will be added to the list
                    for (int i = 0; i < Kunde_dataGridView.SelectedCells.Count; i++)
                    {
                        row.Cells[i].Value = Kunde_dataGridView.SelectedCells[i].Value;
                    }
                    DeletedRowsList.Add(row); // Add the Cloned Row to the list
    

    // 仅将 ROW 从 List 添加到 Datagridview

     // DataSet of the datagridview can be found in the Form Load 
    

    //这里也可以做 "yourDataset.table[0].Rows.Add(value,value.....); advokathusetDataSet.Kunde.Rows.Add(DeletedRowsList[lastindex ].Cells[0].Value, DeletedRowsList[lastindex].Cells[1].Value, DeletedRowsList[lastindex].Cells[2].Value, DeletedRowsList[lastindex].Cells[3].Value, DeletedRowsList[lastindex]。 Cells[4].Value, DeletedRowsList[lastindex].Cells[5].Value, DeletedRowsList[lastindex].Cells[6].Value);

    【讨论】:

      【解决方案2】:

      您可以使用 DataGridView.Columns.Add ,此方法接受 DataGridViewColumn 的对象并将其添加到您的网格视图中......然后您可以使用循环来填充这个新列的值,与您想要的类似正在处理行

      【讨论】:

        【解决方案3】:
        DataTable table = new DataTable();
        table.Columns.Add("First Name", typeof(string));
        table.Columns.Add("Last Name", typeof(string));
        table.Columns.Add("Age", typeof(int));
        
        if (File.Exists("file.txt"))
        {
            List<string[]> rows = File.ReadAllLines("file.txt").Select(x => x.Split(';')).ToList();
            foreach (var row in rows)
            {
                string fName = row[0];
                string lName = row[1];
                int age = -1;
                int.TryParse(row[2], out age);
        
                table.Rows.Add(fName,lName,age);
            }
        }
        dataGridView1.DataSource = table;
        

        【讨论】:

          【解决方案4】:

          你可以这样做:

          public class GridObject
          {
              public string Worker { get; set; }
              public string ItemName { get; set; }
              public int Price { get; set; }
              public int Quantity { get; set; }
          
              public override string ToString()
              {
                  return ItemName;
              }
          
          }
          

          就像其他人说的那样绑定它。

          更多信息,请尝试阅读this

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-09-11
            • 2014-12-16
            • 1970-01-01
            • 1970-01-01
            • 2015-08-29
            • 1970-01-01
            相关资源
            最近更新 更多