1

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

5 Comments

,do i need to change from grid to table?
why?? this is for your table if you want to generate the linkbutton in table and if you wish to dynamically add link in grid view create a templatefield & generate linkbutton in that.
if you are creating new template field for grid put that code in databound of gridview and if using above do where you add columns to table
as i shown my above code,i didn't create any template field for the grid i'm directly assigning the datatable to the grid datasource,so how to add a linkbutton?
I'm created a Hyperlinkcolumn to datatable while i find the column header with name "But" or "ViewDetails" and adding the hyperlinkvolumn instead of the datacolumn,but this hyperlinkcolumn not added as a linkbutton or linkcolumn to the gridview,so how achieve link column in gridview while addding datatable as datasource?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.