comment ajouter un bouton de lien à l'aide de table de données comme source de données pour gridview dynamiquement?

Ici, j'ai fait toutes choses depuis le code-behind.J'ai créé un gridview dynamiquement.

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() == "&nbsp;")
{
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()!="&nbsp;")
{
stock = dr[column[j]].ToString();
}
}
}
}
k++;
dt.Rows.Add(myrow);          
}       
#endregion
column = null;
return dt;      
}

Puis-je ajouter un div à chaque fois comme:

mydiv.Les contrôles.Ajouter(grdnew);

comme ci-dessus, je répétez la boucle pour les différentes tables de données à lier à gridview.Ici, j'ai besoin d'un bouton de lien pour gridview.j'ai donc ajouté une colonne de liens hypertexte de datatable,mais je ne suis pas tout linkbutton dans la grille.J'ai plus de 20 tables de données à lier à la grille.j'ai donc préféré créer la grille de manière dynamique.

InformationsquelleAutor tiru | 2012-01-17