在占位符中动态添加gridview

时间:2014-01-02 04:23:41

标签: c# asp.net gridview

我在asp.net中创建动态gridview。为此,我在占位符中添加了网格视图。我正在垂直获取网格视图。但是我希望水平地使用网格视图。每行四个Gridview。这是我用来创建gridview的代码

   protected void Page_Load(object sender, EventArgs e)
    {
        CreateGrid();
    }
    private void CreateGrid()
    {
        DataSet dsServiceId;
        dsServiceId = FetchServiceId();
        int countServices;
        countServices = dsServiceId.Tables[0].Rows.Count;

        for (int i = 0; i < countServices; i++)
        {
            int serviceid = Convert.ToInt32(dsServiceId.Tables[0].Rows[i]["pServiceID"].ToString());
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
            SqlCommand cmd = new SqlCommand("Ezy_opWiseSaleAll", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@date", DateTime.Now.ToString());
            cmd.Parameters.AddWithValue("@Serviceid", serviceid);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            GridView gv = new GridView();


            gv.ID = "_gridview" + i;
            //Queue q = new Queue();
            //q.Enqueue(i);
            gv.DataSource = ds;

            gv.DataBind();

            PlaceHolder1.Controls.AddAt(i,gv);

        }

    }
    protected DataSet FetchServiceId()
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        SqlCommand cmd = new SqlCommand("select distinct pServiceID from tbProcTransactions",con);
        cmd.CommandType = CommandType.Text;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds;

    } 

请告诉我该怎么做? 谢谢你的任何建议!

3 个答案:

答案 0 :(得分:1)

尝试这样......这只是横向显示gridview的例子。

            int i = 0;
        Table tablee = new Table();
        TableRow row1 = new TableRow();

        while (i < countServices)
        {
            if (i%4==0)
            {
                row1 = new TableRow();

            }

            TableCell cell = new TableCell();
            GridView gv = new GridView();
            gv.ID = i.ToString();
            gv.DataSource = dt;
            gv.DataBind();
            cell.Controls.Add(gv);
            row1.Cells.Add(cell);
            tablee.Rows.Add(row1);
            i++;
        }
        PlaceHolder1.Controls.Add(tablee);        

答案 1 :(得分:0)

试试这个:

private void CreateGrid()
{
        DataSet dsServiceId;
        dsServiceId = FetchServiceId();
        int countServices;
        countServices = dsServiceId.Tables[0].Rows.Count;
        //----
        Table t = new Table();
        TableRow row = new TableRow();
        //---
        for (int i = 0; i < countServices; i++)
        {
            int serviceid = Convert.ToInt32(dsServiceId.Tables[0].Rows[i]["pServiceID"].ToString());
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
            SqlCommand cmd = new SqlCommand("Ezy_opWiseSaleAll", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@date", DateTime.Now.ToString());
            cmd.Parameters.AddWithValue("@Serviceid", serviceid);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView gv = new GridView();
            gv.ID = "_gridview" + i;
            gv.DataSource = ds;
            gv.DataBind();
            //-----
            if (i % 4 == 0)
            {
                row = new TableRow();
            }
            TableCell cell = new TableCell();
            cell.Controls.Add(gv);
            row.Controls.Add(cell);
            t.Controls.Add(row);
            PlaceHolder1.Controls.Add(t);
            //------
        }
}

答案 2 :(得分:0)

网格视图是表格。表是块元素。您需要在表格上设置一些CSS,或者在元素容器上设置它们以将它们放在“相同”的行上。

最快的方法是通过表格,然后将gridview's添加到同一表格行的新单元格中。就像是。 (基本代码)。我已经对“守则”进行了评论,以帮助您完成它。

protected void Page_Load(object sender, EventArgs e)
{
    createTable();
}

/// <summary>
/// Creates the table and appends it to the place holder
/// </summary>
void createTable()
{
    //create the table
    Table table = new Table();
    table.ID = "gvd_table";
    table.CssClass = "gvd_table";

    //create row 1
    TableRow row = new TableRow();
    row.ID = "gvd_table_row_1";
    row.CssClass = "gvd_table_row";

    //get the data
    DataSet dsServiceId;
    dsServiceId = FetchServiceId();
    int countServices;
    countServices = dsServiceId.Tables[0].Rows.Count;

    //the row number
    int rows = 1;

    //the grids per row
    int gridsPerRow = 4;

    //enumerate the row
    for (int i = 0; i < countServices; i++)
    {
        //create our table cell
        TableCell cell = new TableCell();
        cell.ID = string.Format("gvd_table_row_{0}_cell_{1}", rows, i);
        cell.CssClass = "gvd_table_row_cell";

        //get the service ID
        int serviceid = Convert.ToInt32(dsServiceId.Tables[0].Rows[i]["pServiceID"].ToString());

        //create the grid control
        cell.Controls.Add(createGrid(serviceid));

        //add the cell to the row
        row.Cells.Add(cell);

        //Comment the next statement for all grids on one row.
        if (i % gridsPerRow == 0)
        {
            table.Rows.Add(row);
            rows++;
            row = new TableRow();
            row.ID = string.Format("gvd_table_row_{0}", rows);
            row.CssClass = "gvd_table_row";
        }
    }
    table.Rows.Add(row);

    PlaceHolder1.Controls.Add(table);
}

/// <summary>
/// gets the service id
/// </summary>
/// <returns></returns>
DataSet FetchServiceId()
{
    //TODO: Fetch the data
    return new DataSet();
}

/// <summary>
/// create the grid view for a item
/// </summary>
/// <param name="serviceID">the item</param>
/// <returns></returns>
GridView createGrid(int serviceID)
{
    //TODO: Create your grid view
    return new GridView();
}