Iam trying to add items from textboxes to radlistview control using a button

2 Answers 45 Views
ListView
Joseph
Top achievements
Rank 1
Joseph asked on 14 Sep 2023, 04:17 PM | edited on 14 Sep 2023, 04:24 PM

In my code, i want to add many items in radlistview and then save to sql server table in windows forms app. Here below is my code, but this code isnt working as radlistview does not contain definition for text

The ones i have lebelled in green color. 

Note: This code normally works with normal windows list view control, but i want to use radlistview to implement the same.


    if(ListView1.Items.Count == 0)
    {
        ListViewItem lst = new ListViewItem(txtCustomer.Text);
        lst.SubItems.Add(txtPhone.Text);
        lst.SubItems.Add(txtAddress.Text);
        lst.SubItems.Add(txtLoadNo.Text);
        lst.SubItems.Add(txtDriver.Text);
        lst.SubItems.Add(txtDriverPhone.Text);
        lst.SubItems.Add(txtHauler.Text);
        lst.SubItems.Add(txtPlateNo.Text);
        lst.SubItems.Add(txtProduct.Text);
        lst.SubItems.Add(txtDescription.Text);
        lst.SubItems.Add(txtQty.Text);
        lst.SubItems.Add(txtDate.Text);
        ListView1.Items.Add(lst);


        txtProduct.Text = "";
        txtDescription.Text = "";
        txtQty.Text = "";

        return;
    }

    for(int j = 0; j <= ListView1.Items.Count - 1; j++)
    {
        if(ListView1.Items[j].SubItems[1].Text == txtCustomer.Text)
        {
            ListView1.Items[j].SubItems[1].Text = txtCustomer.Text;
            ListView1.Items[j].SubItems[2].Text = txtPhone.Text;
            ListView1.Items[j].SubItems[3].Text = txtAddress.Text;
            ListView1.Items[j].SubItems[4].Text = txtLoadNo.Text;
            ListView1.Items[j].SubItems[5].Text = txtDriver.Text;
            ListView1.Items[j].SubItems[6].Text = txtDriverPhone.Text;
            ListView1.Items[j].SubItems[7].Text = txtHauler.Text;
            ListView1.Items[j].SubItems[8].Text = txtPlateNo.Text;
            ListView1.Items[j].SubItems[9].Text = txtProduct.Text;
            ListView1.Items[j].SubItems[10].Text = txtDescription.Text;
            ListView1.Items[j].SubItems[11].Text = txtQty.Text;
            ListView1.Items[j].SubItems[12].Text = txtDate.Text;


            txtProduct.Text = "";
            txtDescription.Text = "";
            txtQty.Text = "";

            return;
        }
    }

    ListViewItem lst1 = new ListViewItem(txtCustomer.Text);
    lst1.SubItems.Add(txtPhone.Text);
    lst1.SubItems.Add(txtAddress.Text);
    lst1.SubItems.Add(txtLoadNo.Text);
    lst1.SubItems.Add(txtDriver.Text);
    lst1.SubItems.Add(txtDriverPhone.Text);
    lst1.SubItems.Add(txtHauler.Text);
    lst1.SubItems.Add(txtPlateNo.Text);
    lst1.SubItems.Add(txtProduct.Text);
    lst1.SubItems.Add(txtDescription.Text);
    lst1.SubItems.Add(txtQty.Text);
    lst1.SubItems.Add(txtDate.Text);
    ListView1.Items.Add(lst1);

    txtProduct.Text = "";
    txtDescription.Text = "";
    txtQty.Text = "";
} catch(Exception ex)
{
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

2 Answers, 1 is accepted

Sort by
0
tar
Top achievements
Rank 2
Iron
Iron
Iron
answered on 15 Sep 2023, 01:03 AM

Holy...

1. Instead of arrays, you should use entity classes to store your properties and to handle the business logic according to your database scheme.

2. Create a presentation class where multiple entities and their properties are combined. Here: 1 customer has multiple loading orders and 1 loading order has multiple products with quantities, each product has a description, etc.

3. By using a List of your presentation class as DataSource of a RadGridView the columns are automatically created and you can directly work with the single object (the customer or loading order item or whatever you have in mind) and adjust it, etc.

I've attached a simple project to give you a start. Here the list is assigned to 2 RadGridViews (1 for direct editing, 1 for display only) and additionally added a RadTextBox where you could also see and adjust the drivers name of the corresponding selected entry.

Good Luck!

0
Nadya | Tech Support Engineer
Telerik team
answered on 15 Sep 2023, 11:19 AM

Hello, Joseph,

The provided information and a picture are greatly appreciated.

@ M., S., thank you for your answer and project.

According to the provided picture, it seems that you want to use RadListView in Details view to display what you have in the text boxes above. The DetailsView provides a grid-like interface for displaying items with more than one data field. @M., S. gave an example by using two RadGridViews. However, it is up to you to choose which control to use.

Note, RadListView supports binding mechanism that automatically fills the list with items based on the provided data structure. I can suggest to use the DataSource property, then set the ValueMember and DisplayMember properties:

  • DataSource - Specifies the source of the data to be bound.
  • DisplayMember - Specifies the particular data to be displayed in a RadListView.
  • ValueMember - Specifies the particular data to be returned as the value of a RadListView.

I hope this information helps. Let me know if you have any other questions.

Regards,
Nadya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
ListView
Asked by
Joseph
Top achievements
Rank 1
Answers by
tar
Top achievements
Rank 2
Iron
Iron
Iron
Nadya | Tech Support Engineer
Telerik team
Share this question
or