简体   繁体   English

如何将Winform的ListView转换为WPF的ListView

[英]how to convert the listview of winform to listview of wpf

I am new to wpf. 我是wpf的新手。 I have this code in my winform application and I want to have this code in wpf ListView .. But I don't know how to start..can someone there help to convert this in wpf ListView here is my code in winform 我在winform应用程序中有此代码,我想在wpf ListView有此代码。.但我不知道如何启动..有人可以帮助在wpf ListView进行转换吗?这是我在winform中的代码

Below code show the value of the listed in item from the database.. 下面的代码显示了数据库中项目中列出的值。

for (int i = 0; i <= ListView1.Items.Count - 1; i++)
{
      con = new SqlConnection(cs);
      string cd = "insert Into ProductSold(InvoiceNo,ProductID,Quantity,Price,TotalAmount) 
                        VALUES (@InvoiceNo,@ProductID,@Quantity,@Price,@Totalamount)";
      cmd = new SqlCommand(cd);
      cmd.Connection = con;
      cmd.Parameters.AddWithValue("InvoiceNo", txtInvoiceNo.Text);
      cmd.Parameters.AddWithValue("ProductID", ListView1.Items[i].SubItems[1].Text);
      cmd.Parameters.AddWithValue("Quantity", ListView1.Items[i].SubItems[4].Text);
      cmd.Parameters.AddWithValue("Price", ListView1.Items[i].SubItems[3].Text);
      cmd.Parameters.AddWithValue("TotalAmount", ListView1.Items[i].SubItems[5].Text);
      con.Open();
      cmd.ExecuteNonQuery();
      con.Close();
}
for (int i = 0; i <= ListView1.Items.Count - 1; i++)
{
      con = new SqlConnection(cs);
      con.Open();
      string cb1 = "update Product set QtyAvailable = QtyAvailable- " + ListView1.Items[i].SubItems[4].Text + " where ProductID= '" + ListView1.Items[i].SubItems[1].Text + "'";
      cmd = new SqlCommand(cb1);
      cmd.Connection = con;
      cmd.ExecuteNonQuery();
      con.Close();
}

You can start from this link . 您可以从此链接开始。 In WPF you need set ItemsSource propery of your ListView and you can create one class for items. WPF您需要设置ListView ItemsSource属性,并且可以为项目创建一个类。 it can include your requirements ( ProductID, Quantity, Price, TotalAmount,... ) as some Properies . 它可以将您的要求( ProductID, Quantity, Price, TotalAmount,... )包括为一些Properies Then there is not SubItem in WPF unlike WinForm . 那么WPF 就不会WinForm那样 SubItem了。 You should create that class (for example MyInvoiceClass ) and create a list of it's objects to use it as ListView Items. 您应该创建该类(例如MyInvoiceClass )并创建其对象列表以将其用作ListView Items。

You can look at this sample: 您可以查看以下示例:

List<MyItemClass> items = new List<MyInvoiceClass>();
items.Add(new MyInvoiceClass() { InvoiceNo = "10", ProductID = 42, .... });
items.Add(new MyInvoiceClass() { InvoiceNo = "11", ProductID = 39, .... });
items.Add(new MyInvoiceClass() { InvoiceNo = "12", ProductID = 7, .... });

//Assign list of objects (Invoice List) to ItemsSource property
ListView1.ItemsSource = items;

Then you can use it like the following code and base on the loop in your question 然后,您可以像下面的代码一样使用它,并基于问题中的循环

foreach (MyInvoiceClass invoice in ListView1.ItemsSource)
{
    // NOW use the "invoice" object to access its properties
}

Please consider @PeterDuniho points, in the first comment on your question, good luck.. 在您的问题的第一条评论中,请考虑@PeterDuniho的观点,祝您好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM