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);
}
}
Related
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;
}
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);
}
i am trying to create a 8*8 gridview but the columns & rows aren't coming .. my code is like this ..
public static DataGridView[] grid = new DataGridView[30];
public DataGridViewImageColumn col;
grid[i] = new DataGridView();
grid[i].Visible = true;
grid[i].AllowUserToAddRows = false;
grid[i].AllowUserToDeleteRows = false;
grid[i].AllowUserToOrderColumns = false;
grid[i].AllowUserToResizeRows = false;
grid[i].AllowUserToResizeColumns = false;
grid[i].ColumnHeadersVisible = false;
grid[i].RowHeadersVisible = false;
grid[i].Location = new System.Drawing.Point(120,5);
grid[i].Size = new System.Drawing.Size(128, 128);
grid[i].BackgroundColor = Color.SeaShell;
grid[i].GridColor = Color.Green;
grid[i].ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
grid[i].CellBorderStyle = DataGridViewCellBorderStyle.Single;
for (j = 1; j <= 8; j++)
{
col = new DataGridViewImageColumn();
col.Width = 2;
col.ImageLayout = DataGridViewImageCellLayout.Normal;
grid[i].Columns.Add(col);
}
for (k = 1; k <= 8; k++)
{
grid[i].Rows.Add();
}
epnl[i].Controls.Add(grid[i]);
here epnl is a panel. i am trying to set the height of every cell as 2 and width as 2 as well but the cells are not coming .. i am only getting an empty datagridview pls help
This is basically because your not adding anything.
You need to create Row object :-
DataGridViewRow row = new DataGridViewRow();
for (k = 1; k <= 8; k++)
{
grid[i].Rows.Add(row);
}
I think this should help.
I write this codes :
TabContainer TabContainer1 = new TabContainer();
TabContainer1.ID = "TabContainer1";
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
for (int toplam_grp = 0; toplam_grp < 1; toplam_grp++)
{
Panel my_panel= new Panel();
my_panel.ID = toplam_grp + "my_panel";
for (int j = 0; j < 10; j++)
{
Label my_label = new Label();
my_label.ID = j + 10 * toplam_grp + "mylabel";//burda 10 j nin sınırı olcak
my_label.Text = "asdasdas";
RadioButtonList radioButtonList = new RadioButtonList();
radioButtonList.ID = j + 10 * toplam_grp + "radioButton";
radioButtonList.RepeatDirection = System.Web.UI.WebControls.RepeatDirection.Horizontal;
for (int i = 0; i < 5; i++)
{
radioButtonList.Items.Add(((char)(i + 65)).ToString());
}
my_panel.Controls.Add(my_label); /////////////////////////burda yanyana gözükse daha iyi olur
my_panel.Controls.Add(radioButtonList);
}
TabPanel tab = new TabPanel();
tab.ID = toplam_grp+"tab1";
tab.HeaderText =toplam_grp+"nbr";
TabContainer1.Tabs.Add(tab);
TabContainer1.Tabs[toplam_grp].Controls.Add(my_panel);
}
cell.Controls.Add(TabContainer1);
row.Cells.Add(cell);
myplace.Rows.Add(row);
I create a tabcontainer and one tab and in tab I create 10 radiobuttonlist(I give them ids each one) which have 5 members.
And I write this code to reach radiobuttonlist but ı dont reach because dont find their ids :
string ss = "";
for (int i = 0; i < 10; i++)
{
ss += ((RadioButtonList)(FindControl(i + "radioButton"))).SelectedIndex + "\n";
}
MessageBox.Show(ss);
for example first radiobuttonlist id : 0radioButton but it cant found.What can I do.Thanks for answering...
The whole code is a little confusing for me but I try to add my 2 cents.
You need to execute FindControl against the RadiobuttonList container and not against the page.
So, for instance, if you added a control named RadioButtonList1 to a asp.net panel named Panel1 you would have to do
Panel1.FindControl("RadioButtonList1 ")
I'm trying to add N number of columns for each days of a given month:
var daysCount = DateTime.DaysInMonth(DateTime.Now.Year, month);
for (int i = 1; i <= daysCount; i++)
{
dataGridView1.Columns.Add(new DataGridViewColumn() { HeaderText = i.ToString() });
}
I'm getting this error:
At least one of the DataGridView
control's columns has no cell
template.
When you create a new datagridview column it's pretty blank. You will need to set the celltemplate of the column so that it knows what controls to show for the cells in the grid. Alternatively I think if you use some of the stronger typed columns (DataGridViewTextBoxColumn) then you might be ok.
The problem stems from your DataGridViewColumn.CellTemplate not being set.
For this scenario a DataGridViewTextBoxCell as the CellTemplate should suffice.
var daysCount = DateTime.DaysInMonth(DateTime.Now.Year, 1);
for (int i = 1; i <= daysCount; i++)
{
dataGridView1.Columns.Add(new DataGridViewColumn() { HeaderText = i.ToString(), CellTemplate = new DataGridViewTextBoxCell() });
}
You need to specify first whether it's a textbox column or combobox column
Try this it will work
var daysCount = DateTime.DaysInMonth(DateTime.Now.Year, month);
for (int i = 1; i <= daysCount; i++)
{
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = i.ToString() });
}
set your table and add needed columns.
then use:
var daysCount = DateTime.DaysInMonth(DateTime.Now.Year, 1);
for (int i = 0; i <= daysCount; i++)
{
i = dataGridView1.Rows.Add(new DataGridViewRow());
dataGridView1.Rows[i].Cells["YourNameCell"].Value = i.ToString();
}
Frist row is 0, not 1. probabily your error are these.