Trying to cut off time in datetime

Go To StackoverFlow.com

0

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

2012-04-05 15:15
by Eddy Whitaker


0

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

2012-04-12 14:14
by Eddy Whitaker


3

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?

2012-04-05 15:17
by Ilya Kogan
Answer or another question? : - huMpty duMpty 2012-04-05 15:29
well according to the count..i have nothing in the collection..but i dont understand how i can have data show up..and it not be in the collection..i'm probably doing something wrong..but not sure wha - Eddy Whitaker 2012-04-05 15:45


3

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}.

2012-04-05 15:23
by Olivier Jacot-Descombes
well according to the count..i have nothing in the collection..but i dont understand how i can have data show up..and it not be in the collection..i'm probably doing something wrong..but not sure wha - Eddy Whitaker 2012-04-05 15:44
If you try to set the format-string BEFORE you add data to your GridView with the AutoGenerateColumns property set, then it will not have columns yet - Olivier Jacot-Descombes 2012-04-05 16:10
i set autogenerate to false..but then my whole table disappeared ..tried the formatting string..still go error..but when i take the formatting string and autogen=false out..table works fine...minus the appearence of thedate - Eddy Whitaker 2012-04-05 16:40
If AutoGenerateColumns is true the columns will be added automatically; however you will have to format the dates after having added the data - Olivier Jacot-Descombes 2012-04-05 16:49
ok then...what is the best way to do that with it being a datetime? i guess i could refer you to that particular question from yesterday http://stackoverflow.com/questions/10014586/need-only-date-in-field-in-a-table-not-full-datetim - Eddy Whitaker 2012-04-05 19:23
actually..let me ask you this..do you think it would be easier to just redo the page, and build the table in gridview...or do you think you may know of a way to change format it after having added dat - Eddy Whitaker 2012-04-05 19:38
I updated my answer with a quick GridView tutorial for data binding - Olivier Jacot-Descombes 2012-04-05 23:22
@EddyWhitaker When someone gives you a good answer, you can upvote or accept it - Ilya Kogan 2012-04-11 11:36
@IlyaKogan yea, i know how it works...been out of town..1st day back today...so i'm just starting to read through everything.but thank - Eddy Whitaker 2012-04-12 14:13


0

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.

2012-04-05 15:23
by TonyStark
yea..i understood what the error meant..just wasnt sure why i was getting it..also i'm pretty sure a duck is in the fourth bucke - Eddy Whitaker 2012-04-05 15:47
Ads