Add a Control to a Datarow Item

Hi
In my code below I bind a Datatable to a list of documents in Sharepoint.

The problem is that when I do
row["ColumnName"] =
I can only assign a string value.

Is there a way that I could as a control (say an ASP.NET Hyperlink etc)?

Thanks
DataTable table = new DataTable();
            table.TableName = "Documents";

            //table.Columns.Add("Type");
            table.Columns.Add("Title");
            table.Columns.Add("ShortDescription");
            table.Columns.Add("Author");
            table.Columns.Add("Date");

            SPListItemCollection items = GetItems();

            try
            {

                if (items.Count == 0)
                {
                }

                foreach (SPListItem item in items)
                {
                    DataRow row = table.NewRow();

                    //Get icon image and add as type
                    string itemUrl = null;
                    string imageUrl = null;
                    itemUrl = item.Web.Url + "/" + item.Url;
                    //imageUrl = Utility.GetIcon(item["Type"].ToString());
                    imageUrl = "";
                    imageUrl = "<img border=\"0\" src=\"" + imageUrl + "\" alt=\"" + itemUrl + "\" >";
                    //row["Type"] = "<a href=\"" + itemUrl + "\">" + imageUrl + "</a>";

                    //Get title
                    row["Title"] = "<a href=\"" + itemUrl + "\">" + item["Title"] + "</a>";

                    //Get Author - could be empty so cater for it
                    //if (item["_Author"] == null)
                    if (item["Author0"] == null)
                    {
                        row["Author"] = String.Empty;
                    }
                    else
                    {
                        row["Author"] = item["Author0"].ToString();
                    }

Open in new window

hmcgeehanAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
robastaConnect With a Mentor Commented:
Look at it this way, the DataTable only holds the values (it is like the table in your database). The Gridview (or whichever control  you are binding to the Datatable), should hold the controls and the formatting.

The following will give you the results on the attached image.


Add this Gridview to the Aspx Page:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:HyperLinkField DataNavigateUrlFields="Url"
                DataNavigateUrlFormatString="{0}" DataTextField="Title" Text="Title" HeaderText="Title" />
            <asp:BoundField HeaderText="Short Description" DataField="ShortDescription"  />
            <asp:BoundField HeaderText="Author" DataField="Author" />
            <asp:BoundField HeaderText="Date" DataField="Date"/>
        </Columns>
    </asp:GridView>


Then add the following to the Code Behind:


protected void Page_Load(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            table.TableName = "Documents";

            //table.Columns.Add("Type");
            table.Columns.Add("Title");
            table.Columns.Add("ShortDescription");
            table.Columns.Add("Author");
            table.Columns.Add("Date");

            table.Columns.Add("Url");

           

            DataRow row = table.NewRow();
            row["Title"] = "This is the Title";
            row["ShortDescription"] = "This is the sample short descr";
            row["Author"] = "Experts Exchange author";
            row["Date"] = DateTime.Now.ToShortDateString();
            row["Url"] = "http://www.experts-exchange.com"; //note that we do not need that <a> tag here. Also, This Column is not displayed on the page but is bound to the URL field of the link.

            table.Rows.Add(row);
            GridView1.DataSource = table;
            GridView1.DataBind();

        }

Open in new window

DatatableHyperlink.PNG
0
 
rashmi_vaghelaCommented:
0
 
hmcgeehanAuthor Commented:
So there it suggests to use a Table ...
How would I modify my code to use a 'Table' instead of a 'DataTable'?


In my code I do this ....

DataTable myDataProvider = GetDatatable();

                view = new DataView(myDataProvider);

                //grid is of type SPGridView
                grid.DataSource = view;
                grid.DataBind();
               
then

private DataTable GetDatatable()
        {
            DataTable table = new DataTable();
            table.TableName = "Documents";

            //table.Columns.Add("Type");
            table.Columns.Add("Title");
            table.Columns.Add("ShortDescription");

etc etc (as in the code snippet above)

0
 
hmcgeehanAuthor Commented:
So I'm trying this ....

//Populate the datatable
                //DataTable myDataProvider = GetDatatable();
                Table myDataProvider = GetDatatable();

                if (myDataProvider.Rows.Count > 0)
                {
                    view = new DataView(myDataProvider);

but the line

                    view = new DataView(myDataProvider);

gives me the error ...

Error      19      The best overloaded method match for 'System.Data.DataView.DataView(System.Data.DataTable)' has some invalid arguments
0
 
hmcgeehanAuthor Commented:
Can anyone help?
As far as I can see a DataTable will only allow me to have string values for each 'cell'
Is that right?
thanks
0
All Courses

From novice to tech pro — start learning today.