Reputation: 275
I am creating dynamic gridview in asp.net. For this I am adding gridviews in placeholder.I am getting gridviews vertically.But i want the gridview horizontally. Four Gridview in a row each.Here is the code which I am using to create the 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;
}
Please tell me how can I do this? Thanks for any suggestion!
Upvotes: 0
Views: 2780
Reputation: 2163
Try like this...It is just example how display gridview in horizontally..
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);
Upvotes: 1
Reputation: 9424
Try this:
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);
//------
}
}
Upvotes: 0
Reputation: 12683
Grid views are tables. Tables are block elements. You will need to set some CSS on the table and \ or the element container to do place them on the "same" row.
The quickest way is through a table, then add the gridview's to a new cell on the same table row. Something like. (basic code). I have commented the Code to help you through it.
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();
}
Upvotes: 0