0

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


    }

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

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

4 Answers 4

1

Win Forms add Datagridview Row to list and Add Row to datagridview from List

// ADD ROW TO LIST

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

// Insert row from list to datagridview

    // Undo  Button
            private void undo_button_Click(object sender, EventArgs e)
            {


            if(DeletedRowsList.Count > 0)
            {
                int lastindex = DeletedRowsList.Count - 1;

// DataSet of the datagridview can be found in the 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

            }


        }

// Only ADD Datagridview Row TO LIST

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

// Only Adding ROW from List to Datagridview

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

//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);

Sign up to request clarification or add additional context in comments.

Comments

0

you could use DataGridView.Columns.Add , this method accepts an object of DataGridViewColumn and add it to your grid view .... then you can use a loop to fill the value of this new column with whatever you want similar to what you are doing with rows

Comments

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

Comments

0

You could do it like this:

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

}

and just bind it like other have said.

More info, try to read this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.