c#里面怎么样动态定义Table列名?

yusi008 2010-10-06 01:23:19
各位高手 我想问一下 在BS结构里面 怎么样给 Table 动态定义表格列名 本来我是手动定义的代码如下
private void AddPostitleHead(Table tab)
{

TableRow tr = new TableRow();
TableCell tdPotiler = new TableCell();
TableCell tdPostilTime = new TableCell();
TableCell tdPotilType = new TableCell();
TableCell tdPotilContent = new TableCell();
TableCell tdAttachFiles = new TableCell();
TableCell tdTime = new TableCell();


tdPotiler.Text ="批阅人";
tdPotiler.HorizontalAlign = HorizontalAlign.Center;
tdPotiler.Width = Unit.Percentage(20);


tdPostilTime.Text ="批阅时间";
tdPostilTime.HorizontalAlign = HorizontalAlign.Center;
tdPostilTime.Width = Unit.Percentage(20);

tdPotilType.Text ="批阅类型";
tdPotilType.HorizontalAlign = HorizontalAlign.Center;
tdPotilType.Width = Unit.Percentage(10);


tdPotilContent.Text ="批阅内容";
tdPotilContent.HorizontalAlign = HorizontalAlign.Left ;
tdPotilContent.Width = Unit.Percentage(30);

tdAttachFiles.Text ="附件";

tdAttachFiles.HorizontalAlign = HorizontalAlign.Center ;
tdAttachFiles.Width = Unit.Percentage(10);

tdTime.Text ="用时(分)";
tdTime.HorizontalAlign = HorizontalAlign.Center ;
tdTime.Width = Unit.Percentage(10);


tr.Height =22;
tr.BackColor = Color.FromArgb(0xe8,0xf4,0xff);

tr.Cells.Add(tdPotiler);
tr.Cells.Add(tdPostilTime);
tr.Cells.Add(tdPotilType);
tr.Cells.Add(tdPotilContent);
tr.Cells.Add(tdAttachFiles);
tr.Cells.Add(tdTime);


tab.Rows.Add(tr);


tdPotiler = null;
tdPostilTime = null;
tdPotilType = null;
tdPotilContent = null;

tr = null;

}
现在想我改成从数据库里面读取列名来定义 这样对定义Table列名会灵活一点可以根据列的多少自动确定列名 但不知道代码该怎么写 还请各位高手帮忙
...全文
337 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yusi008 2010-10-09
  • 打赏
  • 举报
回复
为什么不能将 帖子提前
yusi008 2010-10-08
  • 打赏
  • 举报
回复
为什么不能将 帖子提前
yusi008 2010-10-07
  • 打赏
  • 举报
回复
为什么帖子不能提前
yusi008 2010-10-06
  • 打赏
  • 举报
回复
还有没有更好的方法
湖中仙人 2010-10-06
  • 打赏
  • 举报
回复
tr.columsn.add("列名");
wuyq11 2010-10-06
  • 打赏
  • 举报
回复
<asp:Table ID="HolderTable" runat="server"></asp:Table>
for (int x = 0; x < 10; x++)
{
TableRow row = new TableRow();
for (int y = 0; y < 10; y++)
{
TableCell cell = new TableCell();
Button bt = new Button();
bt.Text = "";
bt.Click += new EventHandler(bt_Click);
cell.Controls.Add(bt);
row.Cells.Add(cell);
}
HolderTable.Rows.Add(row);
}
void bt_Click(object sender, EventArgs e)
{
Button btn=sender as Button;
}
q107770540 2010-10-06
  • 打赏
  • 举报
回复

string queryString =
"SELECT 列名 FROM tb;";
TableRow tr = new TableRow();
using (SqlConnection connection =
new SqlConnection("")
{
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();

SqlDataReader reader = command.ExecuteReader();


while (reader.Read())
{
TableCell td = new TableCell();
td.Text =reader[0];
td.HorizontalAlign = HorizontalAlign.Center;
td.Width = Unit.Percentage(20)
tr.Cells.Add(td);
}


reader.Close();
}
阿彪兄 2010-10-06
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace CIPACE.Include
{
/// <summary>
/// Summary description for Tabulation.
/// </summary>
public partial class ucCostTabulation : System.Web.UI.UserControl
{
private string schema;
private int rows;
private int cols;
private decimal[,] val = new decimal[5, 53];

protected string PageName = "ucCostTabulation";

protected void Page_Load(object sender, System.EventArgs e)
{
Tablu.CellSpacing = 0;
Tablu.CellPadding = 0;
string[] Titles;
Titles = schema.Split(new Char[] { '#' });
char[] delimiters = new char[] { '|' };
string[] colTitles;
colTitles = Titles[0].Split(delimiters);
cols = colTitles.Length;
string[] rowTitles;
rowTitles = Titles[1].Split(delimiters);
rows = rowTitles.Length;
int Method = Convert.ToInt32(HttpContext.Current.Session["DataInputMethod"]);

switch (Method)
{
case 1:
for (int i = 1; i <= rows; i++)
{
for (int j = 2; j <= cols; j++)
{
val[i, j] = 0;
}
}
break;
case 2:
for (int i = 1; i <= rows; i++)
{
decimal sum = 0;
for (int j = 2; j <= cols; j++)
{
sum += val[i, j];
}
val[i, 1] = sum;
}
break;
}
for (int i = 0; i <= rows; i++)
{
TableRow tr = new TableRow();
if (i == 0)
{
tr.CssClass = "head";
}
else
{
tr.CssClass = "row";
}
for (int j = 0; j <= cols; j++)
{
TableCell tc = new TableCell();
if (j == 0)
{
tc.CssClass = "headcolumn";
}
else
{
tc.CssClass = "currency";
}
if (i == 0)
{
if (j > 0)
{
tc.Text = "" + colTitles[j - 1] + "";
}
}
else
{
if (j > 0)
{
TextBox tbx = new TextBox();
tbx.ID = "tbx_" + i.ToString() + "_" + j.ToString();
tbx.MaxLength = 19;
tbx.Text = BLL.Utility.FormatToCurrencyWithoutTail(val[i, j].ToString());

if (!IsEnable)
{
tbx.Enabled = false;
}

tbx.Width = 86;
tbx.MaxLength = 10;
if ((Method == 1 && j == 1) || (Method == 2 && j > 1) || Method == 3)
{
tbx.Attributes.Add("onfocus", "return OnTbxFocus(this, " + i.ToString() + ", " + j.ToString() + ");");
tbx.Attributes.Add("onblur", "return OnTbxBlur(this, " + i.ToString() + ", " + j.ToString() + ");");
tbx.CssClass = "ExcellMoney";
if (Method > 1)
{
tbx.Attributes.Add("onchange", "return OnTbxChange(this, " + i.ToString() + ", " + j.ToString() + ");");
}
}
else
{
tbx.CssClass = "ExcellReadOnlyMoney";
tbx.ReadOnly = true;
}
tc.Controls.Add(tbx);
}
else
{
Label lb = new Label();
lb.ID = "lb" + i.ToString();
lb.Text = rowTitles[i - 1];
lb.Width = 100;
lb.Attributes.Add("value", lb.Text);
tc.Controls.Add(lb);
}
}
tr.Cells.Add(tc);
}
Tablu.Rows.Add(tr);
}

//Generate Client Script Variable
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), @"captialInputMethod", string.Format(@"var captialInputMethod={0};", Method.ToString()), true);
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), @"Cols", string.Format(@"var Cols={0};", cols.ToString()), true);
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), @"Rows", string.Format(@"var Rows={0};", rows.ToString()), true);
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), @"pid", string.Format(@"var pid='{0}';", this.ClientID), true);
}

public bool IsEnable
{
get
{
return ViewState["IsEnable"] == null ? true : Convert.ToBoolean(ViewState["IsEnable"]);
}
set
{
ViewState["IsEnable"] = value;
}
}

public string Schema
{
set
{
schema = value;
}
}

public decimal this[int row, int col]
{
get
{
TextBox tbx;
tbx = (TextBox)this.FindControl("tbx_" + row.ToString() + "_" + col.ToString());
string tx;
tx = tbx.Text.Trim();
return CIPACE.BLL.Utility.FormatToDecimal(tx);
}
set
{
val[row, col] = value;
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{

}
#endregion

}
}

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧