在这里,我从codebehind做了所有事情。我动态地创建了一个gridview。
DataTable dt = null;
dt = loadDynamicGrid(columnname,ds);
GridView grdnew = new GridView();
grdnew.DataSource = dt;
grdnew.DataBind();
private DataTable loadDynamicGrid(string [] column,DataSet ds)
{
#region Code for preparing the DataTable
DataTable dt = new DataTable();
//Create an ID column for adding to the Datatable
DataColumn dcol ;//= new DataColumn(ID1, typeof(System.Int32));
ButtonColumn btncolm;
HyperLinkColumn hplink;
for (int i = 0; i < column.Count(); i++)
{
if (column[i] != "" & column[i]!=null)
{
if (column[i] == "BUY" || column[i] == "Buy" || column[i] == "BuyNow" || column[i] == "Details" || column[i] == "ViewDetails" || column[i] == "BuyQty" || column[i] == "Purchase")
{
//btncolm = new ButtonColumn();
//btncolm.HeaderText = column[i];
//btncolm.Text = column[i];
//dt.Columns.Add(btncolm.Text);
hplink = new HyperLinkColumn();
hplink.HeaderImageUrl = "http://www.google.com";
hplink.HeaderText = column[i];
hplink.Text = column[i];
dt.Columns.Add(hplink.Text);
}
else
{
dcol = new DataColumn(column[i], typeof(System.String));
dt.Columns.Add(dcol);
}
}
}
int k=0;
foreach (DataRow dr in ds.Tables[0].Rows)
{
DataRow myrow = dt.NewRow();
for (int j = 0; j < column.Count(); j++)
{
if (column[j] != "" & column[j] != null)
{
string sdfsd = Convert.ToString(dr[column[j]]);
if (column[j] == "BUY" || column[j] == "Buy" || column[j] == "BuyNow" || column[j] == "Details" || column[j] == "ViewDetails" || column[j] == "BuyQty" || column[j] == "Purchase")
{
//myrow[column[j]] = "http://www.google.com1/";
myrow[column[j]] = dr[column[j]];
}
else
{
if (Convert.ToString(dr[column[j]]).TrimEnd() == " ")
{
myrow[column[j]] = "";
}
else
{
myrow[column[j]] = dr[column[j]];
}
}
if (column[j] == "Stock" || column[j] == "QtyInStock" || column[j] == "Qty" || column[j] == "Available" || column[j].ToLower() == "onhand" || column[j] == "QuantityOnHand" || column[j] == "QtyAvailable" || column[j] == "InStock" || column[j] == "Avail" || column[j] == "Inventory" || column[j] == "Quantity" || column[j] == "Availability")
{
if (Convert.ToString(dr[column[j]]) != "0" && Convert.ToString(dr[column[j]]) != "" && dr[column[j]] != null && Convert.ToString(dr[column[j]]).TrimEnd()!=" ")
{
stock = dr[column[j]].ToString();
}
}
}
}
k++;
dt.Rows.Add(myrow);
}
#endregion
column = null;
return dt;
}
然后我每次都会添加一个div,如:
mydiv.Controls.Add(grdnew);
如上所述我重复循环不同的数据表绑定到gridview.Here我需要一个链接按钮gridview.so我添加了一个超链接列到datatable,但我没有得到任何linkbutton在grid.I有超过20个数据表要绑定到grid.so我更喜欢动态创建网格。
答案 0 :(得分:3)
只需用datagrid替换网格,并将超链接列添加到datatable,并在点击我时将数据提供给该列
DataTable dt=new DataTable();
HyperLinkColumn hplink = new HyperLinkColumn();
hplink.Text = column1[i];
dt.Columns.Add(hplink.Text);
DataRow myrow = dt.NewRow();
myrow[column1[l]] = String.Format("<a href='" + imageUrl(gettablename(tablename)) + "' target='_blank'>" + dr[column[j]] + "</a>");
dt.Rows.Add(myrow);
DataGrid1.DataSource=dt;
DataGrid1.DataBind();
这个问题已经解决了。
答案 1 :(得分:1)
TableCell tc1 = new TableCell();
tc1.Wrap=true;
this.lnkRecvGpNo=new LinkButton();
lnkRecvGpNo.Text="RecvGp No";
this.lnkRecvGpNo.Click+=new EventHandler(lnkRecvGpNo_Click);
tc1.Controls.Add(lnkRecvGpNo);