Adding a table dynamically in code behind I can do something like...
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
cell1.Text = "blah blah blah";
row.Cells.Add(cell1);
myTable.Rows.Add(row);
but what if the order of the table rows depends on the user input....so for each iteration it is either going to be A or B. If it is A, extend row a...If it is B, extend row b
after 1st iteration
A: 123
after 2nd iteration
A: 123
B: abc
after 3rd iteration
A: 123456
B abc
after 4th iteration
A: 123456789
B: abc
Ive tried rows.addAt(index, row); but its not working says index is out of range. is this even the correct way to do this? thanks for any replies
//forloop
{
if(A)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
cell1.Text = "blah blah blah";
row.Cells.Add(cell1);
myTable.Rows.AddAt(0, row);
}
if(B)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
cell1.Text = "blah blah blah";
row.Cells.Add(cell1);
myTable.Rows.Add(1, row);
}
One problem I can see is that each time you are creating a new row/cell instead of getting the reference to an existing row/cell. If I understand correctly, your table has 2 rows, each with 1 cell, and you're trying to grow the contents?
You need to do something more like this. Note that I have not tested this:
TableCell existingCell = myTable.Rows[0].Cells[0];
existingCell.Text += "NewData";
Or if you wanted to add new cells to the existing rows it would be similar:
TableRow existingRow = myTable.Rows[0];
TableCell newCell = new TableCell();
newCell.Text = "NewData";
existingRow.add(newCell);
Related
I'm dynamically adding the rows and columns to a Table, but i am unable to adjust the first column's width.. i tried all possible combinations of "Unit" (new Unit(300, Unit Type.Point) ) - but no joy. It always shows the column-width to length of the data in that column, but i wanted it to be fixed. what's wrong here please.
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc = new TableCell();
tc.ID = "tcResource";
tc.Text = "Resource_Name";
Unit uWidth = new Unit(300, UnitType.Point);
tc.Width = uWidth;
tr.Cells.Add(tc);
for (i = 1; i <= 365; i++)
{
tc = new TableCell();
tc.ID = "tc" + i.ToString();
tc.Text = dtStart.AddDays(i).ToString("dd/MM") ;
dtRange[i - 1] = dtStart.AddDays(i );
tr.Cells.Add(tc);
}
tHoliday.Rows.Add(tr);
Try this
Unit width = new Unit(30, UnitType.Pixel);
TableCell cell = new TableCell();
cell.Width = width;
You should be adjusting the columns width with a CSS style, not by doing it server side. Put this in a CSS file:
td.tcResouce{ width:300px }
and add a link in your ASP.Net page:
<link rel="stylesheet" type="text/css" href="/path/to/file.css">
I have a table column name called QUESTIONTEXT where there are 3 questions in that column. so, there's 3 rows of questions. I am adding those questions to the first column which is working like a charm. Now, i want to add a text box or a checkbox or radio button either next to it in the same cell or to the next column in the same row.
I have 1 question.
1: How to add a control to either in the same row as the text i am currently adding or to the next column in the same row in the table?.
SCENARIO: In the same table i have the column QUESTIONTEXT i also have a TYPEID column with numeric data. Either a 1 or a 2
the idea is that i will know what control to generate for that specific question by the TYPEID number
1 for text
2 for checkbox
I can grab the number like this:
if (dataRow["TYPEID"].ToString() == "1")
So, for the first one its a 1 and i want to add a text box. I just need to generate a text box or checkbox.
Here is how i am currently adding the questions to the first column in the table.
TableRow tableRow;
TableCell tableCell;
foreach (DataTable dataTable in ds.Tables)
{
foreach (DataRow dataRow in dataTable.Rows)
{
tableRow = new TableRow();
tableCell = new TableCell();
TableRow tableRow2 = new TableRow();
TableCell tableCel2 = new TableCell();
tableCell.Text = dataRow["QUESTIONTEXT"].ToString();
if (dataRow["TYPEID"].ToString() == "1")
tableRow.Cells.Add(tableCell);
myTable.Rows.Add(tableRow);
}
}
I also created a enum to aid in the process but not sure if it will help!
public enum typeID : byte
{
text = 1,
multiple_choice = 2
};
Cant someone help? please!
try this for adding second column in same row
TableRow tableRow;
TableCell tableCell;
TableCell tableCell2;
foreach (DataTable dataTable in ds.Tables)
{
foreach (DataRow dataRow in dataTable.Rows)
{
tableRow = new TableRow();
tableCell = new TableCell();
tableCell.Text = dataRow["QUESTIONTEXT"].ToString();
tableCell2 = new TableCell();
switch (dataRow["TYPEID"].ToString())
{
case "1":
Label lbl = new Label();
lbl.Text = "";
tableCell2.Controls.Add(lbl);
break;
case "2":
CheckBox chk = new CheckBox();
chk.Text = "";
tableCell2.Controls.Add(chk);
break;
}
tableRow.Cells.Add(tableCell);
tableRow.Cells.Add(tableCell2);
myTable.Rows.Add(tableRow);
}
}
I know what my fault is, but not sure how to resolve. I am trying to generate an asp:table from Code Behind.
The table should be 3 cells wide... I'll work on the row limit later.
Here's my code:
GallaryImage g = new GallaryImage();
var images = g.GetAll();
photos.Style.Add("width","100%");
photos.Style.Add("border-style","none");
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tr.Cells.Add(tc);
tr.Cells.Add(tc);
tr.Cells.Add(tc);
int cntr = 0;
TableRow row = new TableRow();
foreach (var image in images)
{
cntr++;
TableCell cell = new TableCell();
Image i = new Image();
i.ImageUrl = image.fullThumbPath;
cell.Controls.Add(i);
row.Cells.Add(cell);
if(cntr%3==0)
{
photos.Rows.Add(row);
row.Cells.Clear();
}
}
if(row.Cells.Count > 0)
photos.Rows.Add(row);
}
My problem is that I need to create a new row in the Foreach, only when I need the new row... i.e, when we have added 3 cells.
I thought I could add the row to the table, and then clear the row to start a new row - but that's not working, as I just keep clearing the same row object... and therefore, never add multiple rows.
Can someone assist with my logic here?
GallaryImage g = new GallaryImage();
var images = g.GetAll();
photos.Style.Add("width","100%");
photos.Style.Add("border-style","none");
int cntr = 0;
TableRow row = new TableRow();
foreach (var image in images)
{
cntr++;
TableCell cell = new TableCell();
Image i = new Image();
i.ImageUrl = image.fullThumbPath;
cell.Controls.Add(i);
row.Cells.Add(cell);
if(cntr%3==0)
{
photos.Rows.Add(row);
row = new TableRow();
}
}
if(row.Cells.Count > 0)
photos.Rows.Add(row);
}
I have an asp.net table control like this:
TableHeader
A Text | Textbox
What I want to do is, in the page_load event, duplicate second row with all the controls within it, change the text in the first cell and add as a new row. So here is my code:
for (int i = 0; i < loop1counter; i++)
{
TableRow row = new TableRow();
row = myTable.Rows[1]; //Duplicate original row
char c = (char)(66 + i);
if (c != 'M')
{
row.Cells[0].Text = c.ToString();
myTable.Rows.Add(row);
}
}
But when I execute this code it justs overwrites on the original row and row count of the table doesn't change. Thanks for help....
As thekip mentioned, you are re-writing the reference.
Create a new row. Add it to the grid and then copy the cell values in whatever manner you want.
Something like:
TableRow tRow = new TableRow();
myTable.Rows.Add(tRow);
foreach (TableCell cell in myTable.Rows[1].Cells)
{
TableCell tCell = new TableCell();
tCell.Text = cell.Text;
tRow.Cells.Add(tCell);
}
It gets overwritten because you overwrite the reference. You don't do a copy, essentially the row = new TableRow() is doing nothing.
You should use
myTable.ImportRow(myTable.Rows[1]).
Adjusted based on response try:
row = myTable.Rows[1].MemberwiseClone();
so try this
private TableRow CopyTableRow(TableRow row)
{
TableRow newRow = new TableRow();
foreach (TableCell cell in row.Cells)
{
TableCell tempCell = new TableCell();
foreach (Control ctrl in cell.Controls)
{
tempCell.Controls.Add(ctrl);
}
tempCell.Text = cell.Text;
newRow.Cells.Add(tempCell);
}
return newRow;
}
your code:
for (int i = 0; i < loop1counter; i++)
{
TableRow row = CopyTableRow(myTable.Rows[1]); //Duplicate original row
char c = (char)(66 + i);
if (c != 'M')
{
row.Cells[0].Text = c.ToString();
myTable.Rows.Add(row);
}
}
I have have some code which adds new cells to a table and fills them with text boxes.
The way I've coded it so far works fine:
TableCell tCell1 = new TableCell();
TableCell tCell2 = new TableCell();
TableCell tCell3 = new TableCell();
TableCell tCell4 = new TableCell();
TableCell tCell5 = new TableCell();
TableCell tCell6 = new TableCell();
TableCell tCell7 = new TableCell();
TextBox txt1 = new TextBox();
TextBox txt2 = new TextBox();
TextBox txt3 = new TextBox();
TextBox txt4 = new TextBox();
TextBox txt5 = new TextBox();
TextBox txt6 = new TextBox();
TextBox txt7 = new TextBox();
tCell1.Controls.Add(txt1);
tCell2.Controls.Add(txt2);
tCell3.Controls.Add(txt3);
tCell4.Controls.Add(txt4);
tCell5.Controls.Add(txt5);
tCell6.Controls.Add(txt6);
tCell7.Controls.Add(txt7);
tRow.Cells.Add(tCell1);
tRow.Cells.Add(tCell2);
tRow.Cells.Add(tCell3);
tRow.Cells.Add(tCell4);
tRow.Cells.Add(tCell5);
tRow.Cells.Add(tCell6);
tRow.Cells.Add(tCell7);
As you can see there's basically 4 instructions getting repeated 7 times. I'm sure there has to be a way to accomplish this with just 4 lines of code within a FOR loop and having all the names dynamically assigned but I just can't seem to find anything that would point me in the direction of how to do it.
Something like the following is what I'm after:
for (int i = 0; i < 6; i++)
{
TableCell tCell[i] = new TableCell();
TextBox txt[i] = new TextBox();
tCell[i].Controls.Add(txt[i]);
tRow.Cells.Add(tCell[i]);
}
Any help would be much appreciated.
I think this should do it:
for (int i = 0; i < 7; i++)
{
TableCell tCell = new TableCell();
TextBox txt = new TextBox();
tCell.Controls.Add(txt);
tRow.Cells.Add(tCell);
}
Make sure that 6 is changed to a 7.
This should work fine?
for (int i = 0; i < 6; i++)
{
TableCell tCell = new TableCell();
TextBox txt = new TextBox();
tCell.Controls.Add(txt);
tRow.Cells.Add(tCell);
}
I don't really get what you need the names for though.
Do you plan on using the "txt5" name as a reference to that specific textbox?
Why not just use tRow.Cells[4].Controls[0] As TextBox ?
What you wrote actually looks pretty close to me. There a re a few points to keep in mind though.
I don't believe you need the array index. As long as tRow is initialized outside the loop it will add the new elements each time. You also may want to set the ID property of each textbox so you can access any specific on you may be looking for down the road.
Thanks for all the helpful answers. To those asking questions about what I was doing with the arrays, I wasn't! That was just an example of what I was trying to achieve.
Ian and Lars got the right idea in the fact that I will need to refer to these textboxes later so I just need to use Eugene and Lubos' solution and make sure I'm adding a line that will give them sequential ID's (txt1, txt2 etc) so that I can do this.
Thanks again for all the wonderful (and quick!) input, I'm now in love with this site!