to add dropdown in a table dynamically - c#

I have a table which i cant fill up with data dynamically ... it only tends to show the last table cell the rest rows are empty.The dropdowndownlist seems to popuate in the right way though.
DataTable dtt1 = (DataTable)Session["dbasecolumns"];
int l = dtt1.Columns.Count;
string[] sqlcolumn = new string[l];
***for (int j = 0; j < dtt1.Columns.Count; j++)
{
sqlcolumn[j] = dtt1.Columns[j].ColumnName;
}
Session["excel"] = sqlcolumn;***
DropDownList drd = new DropDownList();
foreach (String colname in sqlcolumn)
{
drd.Items.Add(colname);
}
Table mytable = new Table();
mytable.Visible = true;
for (int rowctr = 0; rowctr < sqlcolumn.Length; rowctr++)
{
TableRow myrow = new TableRow();
mytable.Rows.Add(myrow);
for (int cellctr = 0; cellctr < 1; cellctr++)
{
TableCell mycell = new TableCell();
mycell.Controls.Add(drd);
myrow.Cells.Add(mycell);
}
***mytable.Rows.Add(myrow);***
}
Panel1.Controls.Add(mytable);
Thanks in Advance

DataTable dtt1 = (DataTable)Session["dbasecolumns"];
int l = dtt1.Columns.Count;
string[] sqlcolumn = new string[l];
DropDownList drd = new DropDownList();
foreach (String colname in sqlcolumn)
{
drd.Items.Add(colname);
}
You are iterating over sqlcolumn but your sqlcolumn is empty. It does not have any values to fill up your DropDownList. Here you have defined your string array of a particular length but it does not contains any value. (Hope I am on right direction as I can see only this much of your code).
You can directly fill your DropDownList from a datatable.
DataTable dtt1 = (DataTable)Session["dbasecolumns"];
DropDownList drd = new DropDownList();
for (int i = 0; dtt1 .Rows.Count > i; i++)
{
dhgdh.Items.Add(dt.Rows[i]["dbasecolumns"].ToString());
}
This will work
for (int rowctr = 0; rowctr <= sqlcolumn.Length; rowctr++)
{
TableRow myrow = new TableRow();
mytable.Rows.Add(myrow);
// for (int cellctr = 0; cellctr <= 1; cellctr++)
//{
DropDownList drd = new DropDownList();
foreach (String colname in sqlcolumn)
{
drd.Items.Add(colname);
}
TableCell mycell = new TableCell();
mycell.Controls.Add(drd);
myrow.Cells.Add(mycell);
//}
mytable.Rows.Add(myrow);
}

i added a table with two columns.Each column has a dropdownlist with the listitems from a SQL database and an Excel file.
for (int rowctr = 0; rowctr <= sqlcolumn.Length; rowctr++)
{
mycell = new TableCell();
TableCell mycell1 = new TableCell();
for (int cellctr = 0; cellctr < 1; cellctr++)
{
DropDownList drd = new DropDownList();
DropDownList drd1 = new DropDownList();
foreach (String colname in sqlcolumn)
{
drd.Items.Add(colname);
}
foreach (string colnames in excel)
{
drd1.Items.Add(colnames);
}
TableRow myrow = new TableRow();
mycell.Controls.Add(drd);
mycell1.Controls.Add(drd1);
myrow.Cells.Add(mycell);
myrow.Cells.Add(mycell1);
mytable.Rows.Add(myrow);
}
Panel1.Controls.Add(mytable);

Related

Generate Dynamic Rows & Columns with itemtemplate using Webform Gridview

I want to make data entry grid with validations and item template field which consist of multiple Webform controls. like SAP and Ax Dynamics data entry grid.
Kindly help me if you anyone have this kind of sample grid.
//You can store a DataTable in the session state
DataTable table = Session["Table1"] as DataTable;
table = new DataTable();
DataColumn colid = table.Columns.Add("sno", typeof(Int32));
DataColumn l_ID = table.Columns.Add("l_ID", typeof(String));
table.PrimaryKey = new DataColumn[] { colid };
colid.ReadOnly = true;
l_ID.ReadOnly = true;
getlist();
//List<Dictionary<string, string>> list = grid.GRCol();
for (int i = 0; i < list.Count; i++)
{
DataColumn dcol = new DataColumn(list[i]["fieldid"], typeof(string));
table.Columns.Add(dcol);
}
#region rowgenrate
for (int i = 1; i <= 1; i++)
{
DataRow aRow = table.NewRow();
for (int j = 0; j < table.Columns.Count; j++)
{
if (i == 1 && j > 1)
{
string abc = table.Columns[j].ToString();
aRow["sno"] = i;
aRow["l_ID"] = "001";
aRow[abc] = "";
//table.Rows.Add(aRow);
}
else
{
if (j > 1)
{
aRow["sno"] = i;
aRow["l_ID"] = "";
aRow[j] = "";
}`enter code here`
//table.Rows.Add(aRow);
}
}
table.Rows.Add(aRow);
}
#endregion
Session["Table1"] = table;

How can i create col span or row span on dynamic table

public partial class WebForm1 : System.Web.UI.Page
{
}
protected void Page_Load(object sender, EventArgs e)
{
}
public void CreateRuntime_Table()
{
int tblRows = int.Parse(txtrow.Text);
int tblCols = int.Parse(txtcol.Text);
Table tbl = new Table();
tbl.BorderWidth = 3;
tbl.BorderStyle = BorderStyle.Solid;
tbl.ID = "myTable";
for (int i = 1; i <= tblRows; i++)
{
TableRow tr = new TableRow();
for (int j = 1; j <= tblCols; j++)
{
TableCell tc = new TableCell();
TextBox txtbox = new TextBox();
txtbox.Text = "Test Row:" + i + "Test Col:" + " " + j;
//Add the control to the table cell
tc.Controls.Add(txtbox);
tr.Controls.Add(tc);
}
tbl.Rows.Add(tr);
}
form1.Controls.Add(tbl);
}
protected void Unnamed_Click(object sender, EventArgs e)
{
CreateRuntime_Table();
}
I have created a dynamic table using c#. How do I set a colspan on the dynamic table using a textbox?
For example if I place a value 3 in the text box and click on Apply Span button then the colspan of a dynamic table should change accordingly.
Check the Image below.
I am new to c#, pls help
Thanks.
UI Image
HTML col-span and row-span attributes are represented by ColumnSpan and RowSpan properties of TableCell class.
In your code, you should add assigning of those properties to TableCell tc.
public void CreateRuntime_Table()
{
int tblRows = int.Parse(txtrow.Text);
int tblCols = int.Parse(txtcol.Text);
//I would recommend using int.TryParse with some defaults
int colSpan = 0;
int rowSpan = 0;
int.TryParse(tbColspanName.Text, out colSpan);
int.TryParse(tbRowspanName.Text, out rowSpan);
Table tbl = new Table();
tbl.BorderWidth = 3;
tbl.BorderStyle = BorderStyle.Solid;
tbl.ID = "myTable";
for (int i = 1; i <= tblRows; i++)
{
TableRow tr = new TableRow();
for (int j = 1; j <= tblCols; j++)
{
TableCell tc = new TableCell()
{
//assign entered col / row span
ColumnSpan = colSpan,
RowSpan = rowSpan
};
TextBox txtbox = new TextBox();
txtbox.Text = "Test Row:" + i + "Test Col:" + " " + j;
//Add the control to the table cell
tc.Controls.Add(txtbox);
tr.Controls.Add(tc);
}
tbl.Rows.Add(tr);
}
form1.Controls.Add(tbl);
}

Using CSS how to have row of tables followed by row of tables

I have an Array of Tables. For example 6 x 6. and a PlaceHolder.
I need to place the tables 6 next to each other in the PlaceHolder and then a new line of 6 next to each other, etc. What css properties do I add for each grid to achieve this.
I have
LiteralControl ltr = new LiteralControl();
ltr.Text = "<style type=\"text/css\" rel=\"stylesheet\">" + #".fl { float: left}</style>";
this.Page.Header.Controls.Add(ltr);
Table[,] tableArray = new Table[6,6];
for (int j = 0; j < tableArray.GetLength(0); j++)
{
bool first = true;
for (int i = 0; i < tableArray.GetLength(1); i++)
{
if (first)
{
tableArray[j, i].CssClass = "mGrid";
first = false;
}
else
{
tableArray[j, i].CssClass = "fl mGrid";
}
tableArray[j, i].Width = Unit.Percentage(100 / 6);
PlaceHolderTables.Controls.Add(tableArray[j, i]);
}
}
But I do not know how to start a new row and then have 5 next to it etc. I am inexperienced with CSS. The tables has been initialised else where. mGrid is defined elsewhere as well.
You can place those 36 tables inside a table with 6x6.
protected void Page_Load(object sender, EventArgs e)
{
LiteralControl ltr = new LiteralControl();
ltr.Text = "<style type=\"text/css\" rel=\"stylesheet\">" + #".fl { float: left}</style>";
Page.Header.Controls.Add(ltr);
Table main = new Table();
for (int i = 0; i < 6; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < 6; j++)
{
Table table = CreateTable($"{i}x{j}");
TableCell cell = new TableCell();
cell.Controls.Add(table);
row.Controls.Add(cell);
}
main.Controls.Add(row);
}
PlaceHolderTables.Controls.Add(main);
}
private Table CreateTable(string text)
{
TableCell cell = new TableCell();
cell.Controls.Add(new Literal {Text = text });
TableRow row = new TableRow();
row.Cells.Add(cell);
Table table = new Table();
table.Rows.Add(row);
return table;
}

C# DataGridView Colspan

I want to dynamically create a excel-like table with datagridview, but I want to have the option to set colspan to some columns.
The idea is just to display data, the user will not type anything, but it should be in table/spreadsheet look. If I cannot have datagridview with colspan is there any other type of table-like tool which has a colspan?
I other hand the columns will be created dynamically from database query result.
I'm using windows forms.
Any ideas?
You might take a look at the TableLayoutPanel Class, which has a TableLayoutPanel.SetColumnSpan Method.
Here's a code sample, which spans the one of the text boxes on 2 columns:
var dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Value1");
dt.Columns.Add("Value2");
dt.Rows.Add(1, "aa", "xx");
dt.Rows.Add(2, "bb","yy");
dt.Rows.Add(3, "cc", "zz");
tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
tableLayoutPanel1.ColumnCount = 4;
tableLayoutPanel1.AutoSize = true;
for (int i = 0; i < dt.Columns.Count; i++)
{
var l = new Label();
l.Dock = DockStyle.Fill;
l.Text = dt.Columns[i].ColumnName;
tableLayoutPanel1.Controls.Add(l, i, 0);
}
var emptyLabel = new Label();
emptyLabel.Text = "Empty label";
tableLayoutPanel1.Controls.Add(emptyLabel, 4, 0);
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Rows[i].ItemArray.Length; j++)
{
var tb = new TextBox();
tb.Multiline = true;
tb.Dock = DockStyle.Fill;
tb.Text = dt.Rows[i][j].ToString();
tableLayoutPanel1.Controls.Add(tb, j, i+1);
if (i == 1 && j == 2)
tableLayoutPanel1.SetColumnSpan(tb, 2);
}
}

How to add a dynamically link in a dynamically added table

I'm a newbie and struggle to add a link, showed like an img in a dynamically added table.
string search = Search.Text;
IMyData members = new MyData();
DataTable dt = new DataTable();
dt = members.Search(search);
Table t = new Table();
t.ID = "tblTable";
TableRow row = null;
for (int i = 0; i < dt.Rows.Count; i++)
{
HyperLink link = new HyperLink();
row = new TableRow();
for (int j = 0; j < dt.Columns.Count; j++)
{
TableCell cell = new TableCell();
if (j == dt.Columns.Count - 1) //This last field may hava a number
{
if (Convert.ToInt32(dt.Rows[i][j].ToString()) > 0)
{
link.ID = "link" + i + "_" + j;
link.NavigateUrl = "members.aspx?showLease=" + dt.Rows[i][j].ToString();
link.ImageUrl = "img/document.png";
Page.Controls.Add(link); // How to put this in a cell, not on page
}
else
{
cell.Text = dt.Rows[i][j].ToString();
}
}
row.Cells.Add(cell);
}
t.Rows.Add(row);
}
pnlTable.Controls.Add(t);
How can I put the Hyperlink to the cell, and not to the Page?
Thanks
You can add control in TableCell the way you are doing in Page. Change your code like this
Page.Controls.Add(link);//Will add control in page
cell.Controls.Add(link);//Will add control in table cell
See below, I changed Page.Controls.Add(link) to cell.Controls.Add(link) and moved your Hyperlink declaration into the cell loop. Otherwise if will only be added in the last cell.
But if I see your code it seems that only the last cell will have link or text because of the j == dt.Columns.Count - 1
for (int i = 0; i < dt.Rows.Count; i++) {
row = new TableRow();
for (int j = 0; j < dt.Columns.Count; j++)
{
HyperLink link = new HyperLink();
TableCell cell = new TableCell();
if (j == dt.Columns.Count - 1) //This last field may hava a number
{
if (Convert.ToInt32(dt.Rows[i][j].ToString()) > 0)
{
link.ID = "link" + i + "_" + j;
link.NavigateUrl = "members.aspx?showLease=" + dt.Rows[i][j].ToString();
link.ImageUrl = "img/document.png";
cell.Controls.Add(link); // How to put this in a cell, not on page
}
else
{
cell.Text = dt.Rows[i][j].ToString();
}
}
row.Cells.Add(cell);
}
t.Rows.Add(row);
}

Categories

Resources