0
<asp:GridView ID="gvData" EmptyDataText="No data to display" ClientIDMode="Static" runat="server" AutoGenerateColumns="false" CssClass="gvData">
    <Columns>
        <asp:TemplateField HeaderText="Content">
            <ItemTemplate>
                <asp:Label ID="lblID" runat="server" ClientIDMode="Static" Text='<%# Eval("Provider") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="In Collection(s)">
            <ItemTemplate>
                <asp:Label ID="lblID" runat="server" ClientIDMode="Static" Text='<%# Eval("Collection") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

C#:

strCollFinalized = ddlContent.SelectedValue + "," + CheckInCollection(Convert.ToInt64(ddlContent.SelectedItem.Value)); //returns two string values
string[] strL = strCollFinalized.Split(',');
gvData.DataSource = strL;
gvData.DataBind();

The above code gives me an error:

DataBinding: 'System.String' does not contain a property with the name 'Provider'.

How can I populate the first string into Provider column and the second string into Collection column.

Si8
  • 9,141
  • 22
  • 109
  • 221
  • do you have the datagrid `Auto Generate Columns` set to false..? – MethodMan Jan 06 '16 at 19:57
  • 1
    try to bind the columns to Container.DataItem instead - Text=<%# Container.DataItem %> – pangular Jan 06 '16 at 19:57
  • Yes it is currently set to `false` – Si8 Jan 06 '16 at 19:57
  • you need to name the split items to the same value as the data in your Eval – MethodMan Jan 06 '16 at 19:58
  • @codeRecap Will it know it is two separate column in the order I use it? – Si8 Jan 06 '16 at 19:58
  • I would change using a string array and use a Class to represent the names of the columns you're trying to bind.. also look at this example for further clarification http://stackoverflow.com/questions/6380449/how-can-bind-gridview-to-array – MethodMan Jan 06 '16 at 20:00
  • 1
    It should! (would be strange if it didn't) – pangular Jan 06 '16 at 20:00
  • @codeRecap it does populate :) except one value is for both column and next value is for both columns too. I don't know if it makes sense on what I said. I want the first value for the first column and second value for the second column. – Si8 Jan 06 '16 at 20:01
  • 2
    ah, now I see. it will populate two rows, of course. if you want to see two columns with distinct values, as one possible (and simplest) solution, you'll have to create an object with two properties, each assigned to items in your array respectively, and then bind the columns to the properties – pangular Jan 06 '16 at 20:08

1 Answers1

2

You are getting that error cause of your Templatefield specifically when you say Text='<%# Eval("Provider") %>. So it's looking for a property/column name Provider in the source which is not present and so the error.

If you want to make this string collection as source for gridview then remove those templatefield altogether and then probably you can say

List<string> strL = strCollFinalized.Split(',').ToList();
gvData.DataSource = strL;
gvData.DataBind();

Another way would be creating a DataTable like

DataTable dt = new DataTable();
DataColumn dc = new Datacolumn("Provider");
DataColumn dc1 = new Datacolumn("Collection");

dt.columns.Add(dc);
dt.columns.Add(dc1);

DataRow dr = dt.NewRow();
dr[dc] = ddlContent.SelectedValue;
dr[dc1] = CheckInCollection(Convert.ToInt64(ddlContent.SelectedItem.Value));

dt.Rows.Add(dr);
gvData.DataSource = dt;
gvData.DataBind();
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • It does work except the first value is shown for both columns and the second value is shown for both columns – Si8 Jan 06 '16 at 20:04
  • @SiKni8, Do a quick look into the edit I just made. will work for you. – Rahul Jan 06 '16 at 20:07
  • Then I will have to set the auto generate column to true. right? – Si8 Jan 06 '16 at 20:10
  • Ahh!!! Yes you will have to. Now it will work even if you don't have those templatefield. – Rahul Jan 06 '16 at 20:11
  • How can I fix the string split? It is not displaying correctly. I have the lines as this: `strCollFinalized = "" + ddlContent.SelectedItem + "," + CheckInCollection(Convert.ToInt64(ddlContent.SelectedItem.Value)) + ""; List strL = strCollFinalized.Split(',').ToList();` – Si8 Jan 06 '16 at 20:17
  • 1
    didn't got you. you mean your split is not working? No need of converting it to list. Keep your code only since now we are making a datatable. – Rahul Jan 06 '16 at 20:20
  • The only issue is my first column also has a `,` so it never gets the third item in the list :/ I will use a `|` character to be safe. I am trying this now. – Si8 Jan 06 '16 at 20:24
  • 1
    @SiKni8, you don't need to split probably. see edit again. You can just assign the values directly. – Rahul Jan 06 '16 at 20:26
  • The only issue I have is the `CheckCollection` function returns a string which is in a forloop like this: `strColl += item.Id.ToString() + " (" + item.Title + ")" + "\r\n";` but the column is not displaying each string in a new line. – Si8 Jan 06 '16 at 20:31
  • @SiKni8, that's interesting. Consider posting it as separate question. You can link this question if you want for reference but it's a separate question altogether. – Rahul Jan 06 '16 at 20:34
  • http://stackoverflow.com/questions/34642308/how-to-show-new-line-in-a-gridview-column – Si8 Jan 06 '16 at 20:37
  • @SiKni8, in that case consider accepting this question since your main issue per this post have been worked out. – Rahul Jan 06 '16 at 20:39