Reputation: 511
I used a gridview
to select the columns from a List dynamically. The lists also have an attachment field. So I have the attachment file url in a variable called attach.
Ex: attach = "http: //sok:1234/Lists/CapabilityTableSales/attachments/1/Dashboard Solutions.pptx"
I used the following Code !!
private void PopulateGrid()
{
try
{
// List is Hard Coded here . we can also use current Context instead//
string SiteListURL = "http://sok:1234/Lists/Case%20Studies/";
using (SPSite oSiteCollection = new SPSite(SiteListURL))
{
using (SPWeb web = oSiteCollection.OpenWeb())
{
DataTable dt = new DataTable();
SPQuery query = new SPQuery();
query.Query = "";
if (txt_Search.Text != String.Empty)
{
txt_Search.Text = "Key1";
query.Query = @"<Where><Contains><FieldRef Name='Key%20words' /><Value Type='Text'>" + txt_Search.Text + "</Value></Contains></Where>";
}
query.ViewFields = String.Concat(
"<FieldRef Name='Title' />",
"<FieldRef Name='Key%20words' />",
"<FieldRef Name='Project%20Brief' />",
"<FieldRef Name='Execution%20Highlights' />");
query.ViewFieldsOnly = true;
SPList list = web.Lists["Case Studies"];
SPListItemCollection col = list.GetItems(query);
dt = web.Lists["Case Studies"].GetItems(query).GetDataTable();
dt.Columns.Add(new DataColumn("Attachment", typeof(HyperLink)));
foreach (DataRow drow in dt.Rows)
{
drow["Attachment"] = getURL(drow["Title"].ToString());
//Where the function getURL will return the attachment URL!! (Working Fine)
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
catch (Exception ex)
{
//throw ex;
}
}
How ever getting a column mismatch error for the line drow["Attachment"] = getURL(drow["Title"].ToString());
My requirement is to open the file (PPTs) on click of that URL.
Many Thanks!!
Upvotes: 1
Views: 622
Reputation: 1456
Sounds like a data type mismatch. I guess your getResult
is returning something other than type : HyperLink
which is what your data row expects.
Upvotes: 1