0

I have performed enormous amount of Google search on this topic but couldn't really find the proper answer to this question. The solution might be simple, but I am a beginner to C# ASP.NET.

I have some code that is taking and storing user inputs from a dropdown list and a textbox into its individual List. I am trying to display both lists in a single gridview as individual columns. For an example, when a user selects a product and type in the quantity and hits the add button, it should display the details in a single row of a gridview. Now I have achieved saving the data into a list but cannot get it to display it in a single row.

Here is my code:

    List<string> productIdList = new List<string>();
    List<string> productTemp = new List<string>();
    List<string> quantityList = new List<string>();
    List<string> quantityTemp = new List<string>();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            productTemp = (List<string>)ViewState["productId"];
            quantityTemp = (List<string>)ViewState["quantity"];

            string str1 = Convert.ToString(productTemp);
            string str2 = Convert.ToString(quantityTemp);

            if (str1 != "")
            {
                if (productTemp.Count != 0)
                {
                    foreach (string ids in productTemp)
                    {
                        productIdList.Add(ids);
                    }
                }
            }

            if (str2 != "")
            {
                if (quantityTemp.Count != 0)
                {
                    foreach (string qtys in quantityTemp)
                    {
                        quantityList.Add(qtys);
                    }
                }
            }
        }
    }

    protected void btnContinue_Click(object sender, EventArgs e)
    {
        productIdList.Add(ddlProduct.SelectedValue.ToString());
        quantityList.Add(txtQuantity.Text);
        ViewState["productId"] = productIdList;
        ViewState["quantity"] = quantityList;
        txtQuantity.Text = "";

        ArrayList testList = new ArrayList();

        testList.AddRange(productIdList);
        testList.AddRange(quantityList);

        grdTest.DataSource = testList;
        grdTest.DataBind();

        grdProduct.DataSource = productIdList;
        grdProduct.DataBind();

        grdQuantity.DataSource = quantityList;
        grdQuantity.DataBind();
    }

}

The gridview currently present are for test purpose to check if data persists after every click of button. grdTest is what I am using for trying to display my list as columns.

Final would be something like this:

Name                     Qty
-----                      -----
Name1(list1)       5(list2)

Thanks!

4

1 回答 1

0

You can use Linq to create List of object with Name and Qty from two lists like below

var temp = productIdList.Zip(quantityList, (n, w) => new { Name = n, Qty = w });

grdTest.DataSource = temp.ToList();
grdTest.DataBind();

Gridview you have to show both name and quantity in one row, if you join name and quantity to a one list it will not display as you expected ( all will show in one column)

we can create new class with Name and Qty as properties and create List of items by iterating though productIdList and quantityList.

Read more about Enumerable.Zip

于 2013-07-25T08:15:50.747 回答