如何使用列表将行添加到datagridview [英] How to add row to datagridview using list

查看:250
本文介绍了如何使用列表将行添加到datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用列表将行添加到datagridwiev的帮助.我上课

I need help adding rows to datagridwiev using list. I have a class

 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; }


    }

我正在制作win表格应用程序,该应用程序可以在datagridview中显示滑动项.现在我做到了这样

And i am making win form app that sould display slod items in datagridview. By now i made it do it like this

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中显示它

It works but I wanted to add a new column(quantity) that is not in database and that counts same itmes sold by one user(worker in my case). I tought doing it with list buy i don't know how to fill the list and than display it in datagridview

推荐答案

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可以在表单加载"中找到

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添加到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);

//Here you can do also "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);

这篇关于如何使用列表将行添加到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆