Eddy Whitaker
Eddy Whitaker

Reputation: 141

Trying to cut off time in datetime

I am having some problems getting only a date to show in my table..

I got some help on a previous question, but ran into another problem. These are the things we have added:

((BoundField)gvEmployeeReview.Columns[1]).DataFormatString = "{0:d}"; //date hired column
((BoundField)gvEmployeeReview.Columns[8]).DataFormatString = "{0:d}"; //next review column

Here is the aspx page http://pastebin.com/DnH3wcAG
and here is the code behind page http://pastebin.com/yY2nbbEG
When I run it, I get:

"Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index" and its on this line "Line 158: ((BoundField)gvEmployeeReview.Columns[1]).DataFormatString = "{0:d}"; //date hired column"

Any help trying to fix this problem would be fantastic. I haven't had any luck

Upvotes: 0

Views: 1056

Answers (4)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112392

This has nothing to do with date formatting. Probably the error occurs while trying to access a column. Note the column indexes a zero-based. If you have N columns, the indexes range from 0 to N-1. Try to use the indexes [0] and [7] instead of [1] and [8]:

((BoundField)gvEmployeeReview.Columns[0]).DataFormatString = "{0:d}";
((BoundField)gvEmployeeReview.Columns[7]).DataFormatString = "{0:d}";

This yields the actual number of columns

gvEmployeeReview.Columns.Count

UPDATE

I am not a web specialist; hovever, I just made a test with a GridView and I got the date formatting right immediately. I worked with an object data source like this:

  1. Create a class with the desired properties. Add a static method returning a List<T> with some real or sample data

    public class Model
    {
        public int ID { get; set; }
        public DateTime BeginDate { get; set; }
        public DateTime EndDate { get; set; }
        public string Name { get; set; }
    
        public static List<Model> GetModels()
        {
            return new List<Model> {
                new Model{ BeginDate=DateTime.Now,
                           EndDate=DateTime.Now.AddDays(1), ID=1, Name="test"},
                new Model{ BeginDate=DateTime.Now.AddDays(10),
                           EndDate=DateTime.Now.AddDays(12), ID=1, Name="test 2"}
            };
        }
    }
    
  2. Place a GridView on the page and open the tasks window with a click on the little [>] attached to upper right of the GridView.

  3. In Choose Data Source... select <New data source...>.

  4. In the Data Source Configuration Wizard windows that opens, select Object and then click OK. Then choose the Model class as business object and click Next >. Finally select GetModels on the SELECT tab and click finish.

  5. After the last step of point 4 a dialog window open with the question "Refresh Fields and Keys for 'GridView1'". Klick Yes. The designer automatically adds columns for every property of selected class and displays some sample data.

  6. Now, click again on [>] and select Edit Columns.... Select a date column in lower left list. You will find the DataFormatString property in the properties window under section Data. Here you can enter {0:d}.

Upvotes: 3

Eddy Whitaker
Eddy Whitaker

Reputation: 141

I just redid the page and built the table in gridview rather than building it in the code...in the end it was just much easier..had to change a bit of the code...but it all turned out good..thanks for all the input

Upvotes: 0

TonyStark
TonyStark

Reputation: 33

Look at your collection datasource, your attempting to read something that doesn't exist. Think of it like this: You have three buckets. Now I ask you what's in the fourth bucket? This error is you telling me I don't have a 4th bucket.

Upvotes: 0

escargot agile
escargot agile

Reputation: 22379

As the exception says, you're accessing a member of a collection that is out of bounds. Probably gvEmployeeReview.Columns[1] does not exist. How many members are there in gvEmployeeReview.Columns?

Upvotes: 3

Related Questions