c# - Dynamically created table and adjusting TextBox length
In the code below (which is in loop - not shown!), the length of the string contained in TextBox is too long. How can I fix this? Also, is it possible to use a Text开发者_运维百科View instead of a TextBox?
TableCell tc = new TableCell();
TextBox txtBox = new TextBox();
txtBox.Text = reader.GetString(col);
// Add the control to the TableCell
tc.Controls.Add(txtBox);
// Add the TableCell to the TableRow
tr.Cells.Add(tc);
col++;
If you would rather have a text, use a Label
instead of TextBox
. Like that:
TableCell tc = new TableCell();
Label label = new Label();
label.Text = reader.GetString(col);
// Add the control to the TableCell
tc.Controls.Add(label);
// Add the TableCell to the TableRow
tr.Cells.Add(tc);
col++;
Now, if you want to have control over the TextBox width, you can just set it:
txtBox.Width = 40; //or whatever value suits your needs
You can use the label control instead of the textbox, if you just want to display to the user the text and don't want them to edit it.
精彩评论