且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

列表框控件

更新时间:2022-06-03 03:21:03

这是您应该能够做到的.

(我创建了示例,假设您需要在GridView中显示一些选定的Person 对象).您只需要根据需要更改以下代码:

Here is how you should be able to do it.

(I created the example assuming you need to show some selected Person objects in the GridView). You just need to change the following code according to your need:

///<summary>
///Load selected Persons in the GridView
///</summary>
private void LoadPersons()
{
    //Get the selected indexes
    int[] indeces = ListBox1.GetSelectedIndices();

    foreach (int index in indeces)
    {
        ListItem item = ListBox1.Items[index];
        int Id = Convert.ToInt32(item.Value);
        //Load person by ID
        Person person = GetPersonById(Id);
        if (person != null)
        {
            persons.Add(person);
        }
    }
    //Bind GridView
    GridView1.DataSource = persons;
    GridView1.DataBind();
}