0

I have a List called dayList which has 12 dates in it. I am trying to Bind this List to a GridView Column.

  GridView2.DataSource = dayList;

        GridView2.DataBind();

        for (int i = 0; i < dayList.Count; i++)
        { 
            Label lbldate = (Label)GridView2.Rows[i].FindControl("lblgriddate");
            lbldate.Text = Convert.ToString(dayList[i]);
        }
    }

I am getting Object reference not set to an instance of an object error at:

lbldate.Text = Convert.ToString(dayList[i]);

Is it because there are no rows in the GridView? But shouldnt GridView automatically add rows just like when we bind GridView Column with DataTable?

SamuraiJack
  • 5,131
  • 15
  • 89
  • 195

3 Answers3

1

As @manish-mishra said, why are you even doing this? Just having the following would be enough:

GridView2.DataSource = dayList;
GridView2.DataBind();

If you want to set the text of your lblgriddate control, you should set it to bind to the DataItem of your rowtemplate.

        var dayList = new List<DateTime>() { 
            DateTime.Today, 
            DateTime.Today.AddDays(-1), 
            DateTime.Today.AddDays(-2) 
        };
        GridView2.DataSource = dayList;
        GridView2.DataBind();

And for HTML:

    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField HeaderText="Date">
                <ItemTemplate>
                    <asp:Label ID="lblgriddate" Text="<%# Container.DataItem %>" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

I then added the following code after the DataBind() call, this also works. It doesn't change anything though. Because the label itself is already databound to the value, you dont have to bind it explicitly in code.

        for (int i = 0; i < dayList.Count; i++)
        {
            Label lbldate = (Label)GridView2.Rows[i].FindControl("lblgriddate");
            lbldate.Text = Convert.ToString(dayList[i]);
        }
w5l
  • 5,341
  • 1
  • 25
  • 43
  • regarding ' it's not a child of the GridView row, it's a child of a specific cell,'.... no Willem, he is getting that error because there is no row, had there been sufficient amount of rows, that's the code he would have to write to Find a Control. – Manish Mishra Mar 07 '13 at 13:09
  • I stand corrected on the FindControl bit, I just made a sample app to test and it did find the label control. But it also shows that the for-loop works too. – w5l Mar 07 '13 at 13:16
0

You are trying to access those rows of GridView, which doesn't even exist.

why are you trying to bind a single column of a gridView??

you should do either of these two:

  1. directly bind your gridView with your dayList i.e.

     GridView2.AutoGenerateColumns=true;
     GridView2.DataSource = dayList;
     GridView2.DataBind();
    
  2. Bind your GridView to a alternate DataSource having atleast of 12 rows, so that your GridView does have rows, and then update one of its column at RowDataBound or just after (dataBinding your Grid) with values from your DayList i.e.

    for (int i = 0; i < dayList.Count; i++)
    { 
        Label lbldate = (Label)GridView2.Rows[i].FindControl("lblgriddate");
        lbldate.Text = Convert.ToString(dayList[i]);
    }
    

inside RowDataBound

    protected void Review_grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[2].Text = DayList[e.Row.RowIndex];

        }
    }

it should give you an idea:

also you can covert your DayList collection into GridViewRow collection and add it to Controls of the GridView. see ref.

Community
  • 1
  • 1
Manish Mishra
  • 12,163
  • 5
  • 35
  • 59
  • Can i just add rows to the Grid dynamically depending on the number of elements in List? Because number of elements can vary in the List. – SamuraiJack Mar 07 '13 at 13:02
  • 1
    No, you cannot add rows directly to a GridView. you can Create a DataTable and add rows into that iterating over your list and then bind that DataTable to GridView. But than in that Case, why not directly bind your DayList to GridView??. You can control headerText, formatting and what not – Manish Mishra Mar 07 '13 at 13:05
  • Right on! "Create a DataTable and add rows into that iterating over your list and then bind that DataTable to GridView." saved the day. – SamuraiJack Mar 08 '13 at 11:06
0

Create a new DataGridViewRow object abd then add your item from you list to row. After doing this Add this row to Your gridview. This will do what you are trying do

vzades
  • 112
  • 2
  • 4