Search code examples
asp.netgridviewdatatablelinkbutton

how to add link button using datatable as datasource to gridview dynamically?


Here i did every thing from codebehind.I created a gridview dynamically.

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;      
}

Then i adding to a div everytime like:

mydiv.Controls.Add(grdnew);

as above i repeate the loop for different table of data to bind to gridview.Here i need a link button to gridview.so i added a hyperlink column to datatable,but i'm not getting any linkbutton in grid.I have more than 20 tables of data to bind to grid.so i preferred to create the grid dynamically.


Solution

  • Just replace the grid with datagrid and add hyperlinkcolumn to datatable and give data to that column as click me

    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();
    

    Thats it problem solved.