I`m creating a dynamic HTML Table, the table content is from the database.
I think I can do it - but I have 2 question:
How I add a <th> to each column? I want to name to columns I have (CustomerId,ProductId, etc).
How can I click on a specific row?
I want to click on a row and to be directed to a different page.
Note - I'm, not allowed to use DataTable - just HTML Table control.
This is my code:
string SQL_Order_View = "SELECT * FROM Orders";
SqlCommand SQLcmdOrders = new SqlCommand(SQL_Order_View, Conn);
Conn.Open();
SqlDataReader myOrders = SQLcmdOrders.ExecuteReader();
while(myOrders.Read())
{
TableRow tr = new TableRow();
TableCell tdCustomerId = new TableCell();
TableCell tdProductId = new TableCell();
TableCell tdPrice = new TableCell();
tdCustomerId.Text = myOrders["CustomerId"].ToString();
tdProductId.Text = myOrders["ProductId"].ToString();
tdPrice.Text = myOrders["Price"].ToString();
tr.Cells.Add(tdCustomerId);
tr.Cells.Add(tdProductId);
tr.Cells.Add(tdPrice);
tblOrders.Rows.Add(tr);
}
Conn.Close();
1.How I add a to each column
// Create a TableHeaderRow.
TableHeaderRow headerRow = new TableHeaderRow();
// Create TableCell objects to contain
// the text for the header.
TableHeaderCell headerTableCell1 = new TableHeaderCell();
TableHeaderCell headerTableCell2 = new TableHeaderCell();
TableHeaderCell headerTableCell3 = new TableHeaderCell();
headerTableCell1.Text = "CustomerId";
headerTableCell2.Text = "ProductId";
headerTableCell3.Text = "Price";
// Add the TableHeaderCell objects to the Cells
// collection of the TableHeaderRow.
headerRow.Cells.Add(headerTableCell1);
headerRow.Cells.Add(headerTableCell2);
headerRow.Cells.Add(headerTableCell3);
// Add the TableHeaderRow as the first item
// in the Rows collection of the table.
tblOrders.Rows.AddAt(0, headerRow);
2.How can to click on a specific row
tr.Attributes["onclick"] = "<some javascript code here>";
string SQL_Order_View = "SELECT * FROM Orders";
SqlCommand SQLcmdOrders = new SqlCommand(SQL_Order_View, Conn);
Conn.Open();
SqlDataReader myOrders = SQLcmdOrders.ExecuteReader();
// adding header
TableHeaderRow headRow = new TableHeaderRow();
TableHeaderCell thCustomerId = new TableCell();
TableHeaderCell thProductId = new TableCell();
TableHeaderCell thPrice = new TableCell();
thCustomerID.Text = "Customer ID";
thProductId.Text = "Product ID";
thPrice.Text = "Price";
tblOrders.Rows.Add(headRow );
while(myOrders.Read())
{
TableRow tr = new TableRow();
TableCell tdCustomerId = new TableCell();
TableCell tdProductId = new TableCell();
TableCell tdPrice = new TableCell();
tdCustomerId.Text = myOrders["CustomerId"].ToString();
//tdProductId.Text = myOrders["ProductId"].ToString();
//create clickable----
HyperLink lnk= new HyperLink();
lnk.ID = myOrders["ProductId"].ToString();
lnk.NavigateUrl = "your url with some id here";
tdProductId.Controls.Add(lnk);
tdPrice.Text = myOrders["Price"].ToString();
tr.Cells.Add(tdCustomerId);
tr.Cells.Add(tdProductId);
tr.Cells.Add(tdPrice);
tblOrders.Rows.Add(tr);
}
Conn.Close();
Use TableHeaderCell.
Additional information: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.tableheadercell.aspx
Related
i try adding rows to table on the fly from DataBase but its always the last row that appears only where am i wrong ?
TableCell pNameCell = new TableCell();
TableCell pDescCell = new TableCell();
TableCell pPriceCell = new TableCell();
TableCell pStockCell = new TableCell();
TableCell buyProduct = new TableCell();
HyperLink hl = new HyperLink();
//ds is DataSet
foreach (DataRow dRow in ds.Tables[0].Rows)
{
TableRow row = new TableRow();
pNameCell.Text = dRow["name"].ToString();
row.Cells.Add(pNameCell);
pDescCell.Text = dRow["description"].ToString();
row.Cells.Add(pDescCell);
pPriceCell.Text = dRow["price"].ToString();
row.Cells.Add(pPriceCell);
pStockCell.Text = dRow["Qty"].ToString();
row.Cells.Add(pStockCell);
hl.Text = "Add To Cart";
hl.NavigateUrl = "BuyProduct.aspx?id="+ dRow["pro_id"].ToString();
hl.CssClass = "btn btn-primary";
buyProduct.Controls.Add(hl);
row.Cells.Add(buyProduct);
//TProducts is asp:table ID
TProducts.Rows.Add(row);
}
it should show all the data rows in able
Your table cells are not unique to each iteration of the loop. When you add a cell variable to a row, it maintains a reference to that variable, not a copy. So then in the next iteration of the loop you overwrite the cell variable with the value from the new row, this will also update all the references to that cell.
To fix it, simply move the table cell declarations inside the loop, then their scope will be limited to that iteration, and new variables will be created each time you loop - just like the table row variable already is, in fact:
//ds is DataSet
foreach (DataRow dRow in ds.Tables[0].Rows)
{
TableCell pNameCell = new TableCell();
TableCell pDescCell = new TableCell();
TableCell pPriceCell = new TableCell();
TableCell pStockCell = new TableCell();
TableCell buyProduct = new TableCell();
HyperLink hl = new HyperLink();
TableRow row = new TableRow();
pNameCell.Text = dRow["name"].ToString();
row.Cells.Add(pNameCell);
pDescCell.Text = dRow["description"].ToString();
row.Cells.Add(pDescCell);
pPriceCell.Text = dRow["price"].ToString();
row.Cells.Add(pPriceCell);
pStockCell.Text = dRow["Qty"].ToString();
row.Cells.Add(pStockCell);
hl.Text = "Add To Cart";
hl.NavigateUrl = "BuyProduct.aspx?id="+ dRow["pro_id"].ToString();
hl.CssClass = "btn btn-primary";
buyProduct.Controls.Add(hl);
row.Cells.Add(buyProduct);
//TProducts is table ID
TProducts.Rows.Add(row);
}
I have table and each row contains a button. If I click the button, I want to retrieve the value from the first column in that particular row. This is my code, how can I do that. Thank you
foreach (var c in list)
{
TableRow row = new TableRow();
TableCell c0 = new TableCell();
c0.Text = string.Format("<img src='"+c.Movie_poster+ "'/>");
TableCell c1 = new TableCell();
c1.Text = c.Name;
TableCell c2 = new TableCell();
c2.Text = c.Date.ToStirng();
TableCell c3 = new TableCell();
c3.Text = c.desc;
TableCell c4 = new TableCell();
c4.Text = c.genre;
TableCell c5 = new TableCell();
//Add each string to cell in row
row.Cells.Add(c0);
row.Cells.Add(c1);
row.Cells.Add(c2);
row.Cells.Add(c3);
row.Cells.Add(c4);
row.Cells.Add(c5);
//Add the row to the table
Table.Rows.Add(row);
Button getname = new Button();
getname.Text = "Reserve";
getname.Click+= new EventHandler(getname_Click);
c5.Controls.Add(getname);
}
}
protected void reserve_Click(object sender,EventArgs e)
{
Button btn = (Button)sender;
TableRow row = (TableRow)btn.
string name = row.Cells[1].Text;
lbl.Text = name;
}
Before create button you should write a new attribute with Row number.
//Add the row to the table
Table.Rows.Add(row);
Button getname = new Button();
getname.Attributes.Add("idRow", idrow);
idrow += 1;
getname.Text = "Reserve";
getname.Click += new EventHandler(getname_Click);
Then in you reserve_Click you should get the row with the ID stored in button attributes
Button btn = (Button)sender;
int idRow = int.Parse(btn.Attributes["id"]);
TableRow row = t.Rows[idRow];
You can bind the value of the Column to the Button as a CommandArgument.
Button getname = new Button();
getname.Text = "Reserve";
getname.CommandName = "Reserve";
getname.CommandArgument = "Column Value";
getname.Command += new CommandEventHandler(Button1_Command);
And then in the Button Command
protected void Button1_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Reserve")
{
string columnValue = e.CommandArgument.ToString();
}
}
Table table = new Table ();
TableRow row1 = new TableRow();
TableRow row2 = new TableRow();
TableCell cell1_1 = new TableCell { RowSpan = 2, };
TableCell cell1_2 = new TableCell
{
Text = string.Format("<h4>{0}</h4><br />{1}<br/>",
product.ProductName, "Product No:" + product.Id),
HorizontalAlign = HorizontalAlign.Left,
};
TableCell cell1_3 = new TableCell { Text = "Unit Price<hr/>"};
TableCell cell1_4 = new TableCell { Text = "Quantity<hr/>" };
TableCell cell1_5 = new TableCell { Text = "Product Total<hr/>"};
TableCell cell1_6 = new TableCell();
TableCell cell2_1 = new TableCell();
TableCell cell2_2 = new TableCell { Text = "Rs " + product.Price};
TableCell cell2_3 = new TableCell();
TableCell cell2_4 = new TableCell { Text = "Rs " + (order.Quantity * product.Price) };
TableCell cell2_5 = new TableCell();
How can i set the width of this table? When i am trying table.Witdh its not working
Table table = new Table();
table.Width=new Unit("100%");
or
table.Width = new Unit("70px");
Where Unit belongs to System.Web.UI.WebControls
table.Width = new Unit("25%")
Works for me.
How can I add button to my DataGridView code, in two cell (c#)
table have 4 columns, and dinamically rows;
last two columns had button.
Code is:
DataTable table = new DataTable();
DataColumn col1 = new DataColumn("Típus (Név)", typeof(string));
DataColumn col2 = new DataColumn("Ertéke", typeof(string));
DataColumn col3 = new DataColumn("Szerkesztés",typeof(Button));
DataColumn col4 = new DataColumn("Törlés",typeof(Button));
table.Columns.Add(col1);
table.Columns.Add(col2);
table.Columns.Add(col3);
table.Columns.Add(col4);
foreach (var item in data)
{
string[] listdata = item.Split('|');
DataRow row = table.NewRow();
string nev = listdata[0];
string ertek = listdata[1];
DataGridViewButtonCell torol = new DataGridViewButtonCell();
torol.Tag = listdata[2];
Button szerkeszt = new Button();
szerkeszt.Text = "Edit";
Button Torol = new Button();
Torol.Text = "Delete";
row[col1] = nev;
row[col2] = ertek;
row[col3] = szerkeszt;
row[col4] = Torol;
table.Rows.Add(row);
}
DataGridView gvTest = new DataGridView();
gvTest.DataSource = table;
gvTest.Size = new Size(700,400);
gvTest.Font = new Font(gvTest.Font.Name,13);
gvTest.ReadOnly = true;
panel.Controls.Add(gvTest);
Try like this
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
gvTest.Columns.Add(btn);
btn.HeaderText = "Click Data";
btn.Text = "Click Here";
btn.Name = "btn";
More Reference here
while (reader.Read())
{
TableRow r = new TableRow();
TableCell c = new TableCell();
c.Controls.Add(new LiteralControl(reader["Name"].ToString()));
r.Cells.Add(c);
Table1.Rows.Add(r);
TableCell c1 = new TableCell();
c1.Controls.Add(new LiteralControl(reader["RollID"].ToString()));
r.Cells.Add(c1);
Table1.Rows.Add(r);
}
i want to add another cell with a dropdownlist for every row.Could any one sort me out with this issue?
You can do like...
DropDownList ddl = new DropDownList();
ddl.ID = "ddl";
ddl.Items.Add(new ListItem("Text", "Value")); // add list items
TableCell c2 = new TableCell();
c2.Controls.Add(ddl);
r.Cells.Add(c2);
Table1.Rows.Add(r);