开发者

how to create a dynamic grid view and a drop down box in its header

开发者 https://www.devze.com 2023-01-15 16:20 出处:网络
how to create a dynamic grid view and a drop down box in its header ? wh开发者_运维问答ere should this be called like in page load ?

how to create a dynamic grid view and a drop down box in its header ?

wh开发者_运维问答ere should this be called like in page load ?

where should the events be handled ?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class GridViewHeaderDDL : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Person[] people = new List<Person>()
        {
            new Person() {ID = 1, name = "John" },
            new Person() {ID = 2, name = "Dave" }
        }.ToArray();

        GridView gvw = new GridView();
        gvw.RowCreated += new GridViewRowEventHandler(gvw_RowCreated);
        gvw.DataSource = people;
        gvw.DataBind();
        this.form1.Controls.Add(gvw);
    }

    void gvw_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            foreach (TableCell itm in e.Row.Cells)
            {
                itm.Text += CreateDDL();
            }
        }
    }

    private string CreateDDL()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<select>");
        sb.Append(@"<option value=""1"">One</option>");
        sb.Append(@"<option value=""2"">Two</option>");
        sb.Append("</select>");

        return sb.ToString();
    }

    public class Person
    {
        public int ID {get; set;}
        public string name { get; set; }
    }
}

Try run this test page on you local and modify from there.

What the code does:

  1. Load dummy data.
  2. Create GridView.
  3. Bind the gridview with dummy data and add to form control.
  4. On RowCreated event, it loads a drop down list in the header cell.

This is the screen shot:

how to create a dynamic grid view and a drop down box in its header

0

精彩评论

暂无评论...
验证码 换一张
取 消