Click here to Skip to main content
15,887,917 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have ItemID, ItemName, Quantity, Date and ItemStatus from inventory table in sql database.


i only want the ItemID and itemName the quantity will be reduced if i want 5 qty from selected item if item has 21 it should be reduce to 16

Capture5 — imgbb.com[^]

when i add selected item from listbox and display it to datagridview it returns system.data.datarowview

What I have tried:

private void Checkout_Load(object sender, EventArgs e)
        {
            LoadData();
        }



        public void LoadData()
        {
            SqlConnection con = Connection.GetConnection();

            SqlDataAdapter sda = new SqlDataAdapter("Select * From [Inventory].[dbo].[Inventory]", con);


            DataTable dt = new DataTable();
            sda.Fill(dt);



            listBox1.DataSource = dt;
            listBox1.DisplayMember = "ItemName";
            listBox1.ValueMember = "ItemID";

            dataGridView1.Rows.Clear();
                                  
            
                foreach (var item in listBox1.SelectedItems)
                {
                    int index = dataGridView1.Rows.Add();
                    dataGridView1.Rows[index].Cells["Column1"].Value = item.ToString();
                    dataGridView1.Rows[index].Cells["Column2"].Value = item.ToString();
                    dataGridView1.Rows[index].Cells["Column3"].Value = textBox3.Text;
                    dataGridView1.Rows[index].Cells["Column4"].Value = textBox1.Text;
                }
            
        }
            




        private void buttonAddtoCart_Click(object sender, EventArgs e)
        {
            LoadData();
        }

       
    }

}
Posted
Updated 18-Oct-19 23:52pm
v3
Comments
Keith Shirogane 19-Oct-19 8:55am    
i know its already solved but i'm just gonna put it here since i can't add another solution


var itemid = (listBox1.SelectedItem as DataRowView)["ItemID"].ToString();
var itemname = (listBox1.SelectedItem as DataRowView)["ItemName"].ToString();


int n = dataGridView1.Rows.Add();

dataGridView1.Rows[n].Cells["Column1"].Value = itemid;
dataGridView1.Rows[n].Cells["Column2"].Value = itemname;
dataGridView1.Rows[n].Cells["Column3"].Value = textBox3.Text;
dataGridView1.Rows[n].Cells["Column4"].Value = textBox1.Text;


}
Richard MacCutchan 19-Oct-19 9:09am    
Thank you for the extra information. As I mentioned below my DataBinding fu is not so good.
Keith Shirogane 19-Oct-19 10:28am    
don't worry about it.. we're all still learning. :D

1 solution

I have looked into this and it appears that because of data binding, the values returned in the SelectedItems list are DataRowView objects. Each of which contains the DataRow of the data source as a member. So the following code will list your items.
C#
foreach (DataRowView rowview in listBox1.SelectedItems)
{
    int index = dataGridView1.Rows.Add();
    var itemid = rowview["ItemID"].ToString();
    var itemname = rowview["ItemName"].ToString();
    dataGridView1.Rows[index].Cells["Column1"].Value = itemid;
    dataGridView1.Rows[index].Cells["Column2"].Value = itemname);
}

I suspect this is not actually the proper way to do it but my knowledge of data binding is rather sketchy at best.

[edit]
Updated with improved code, thanks to Keith Shirogane.
[/edit]
 
Share this answer
 
v3
Comments
Member 14608841 19-Oct-19 5:54am    
i changed object to var in my foreach statement still returns system.data.datarowview
Richard MacCutchan 19-Oct-19 6:49am    
Try changing it to String.
Richard MacCutchan 19-Oct-19 8:11am    
See my updated solution.
Member 14608841 19-Oct-19 8:36am    
Thank you it works.. not to worry about the proper way to do it as long as it works should be fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900