Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,918 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,047 people online right now. Registration is fast and FREE... Join Now!




GridView (DataBinder perhaps?)

 
Reply to this topicStart new topic

GridView (DataBinder perhaps?), Pulling from SQL, and on-the-fly calculations.

gymratz
29 Jan, 2008 - 12:07 PM
Post #1

D.I.C Head
**

Joined: 18 Oct, 2007
Posts: 86


My Contributions
I currently have a GridView that pulls information from a SQL database. My code to do this is at the bottom.

Now what I am TRYING to do is make certain columns not pull from the database but calculate on the fly.
If you can help me with one example I can figure out the rest.
The example I need would be:
DateIn pulls from database.
DateOut pulls from database.

I need to calculate the difference between the two and put it in a column.
I would LIKE to be able to do this using strings if possible (and some conversion) because if I store the dates in SQL as a date/time it attaches 12:00:00am to all my fields - so I'd store as a string and have the user type 10/10/2008.

Hopefully this was clear if not I'll try to say in another way.
I need to store two dates in a database, pull this into a datagrid and get the difference in dates on-the-fly so that I can populate another column of the datagrid.

Thanks!


ASP.Net Page:
CODE
<asp:GridView ID="inout_DataGrid" runat="server" AutoGenerateColumns="False" CellPadding="3"
                                                                    Font-Names="Verdana" Font-Size="X-Small" ForeColor="Black" PageSize="5" Width="902px">
                                                                    <Columns>
                                                                        <asp:BoundField DataField="Quarter" HeaderText="Quarter" SortExpression="Quarter">
                                                                            <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" Width="75px" />
                                                                            <HeaderStyle Font-Bold="True" ForeColor="Black" VerticalAlign="Top" />
                                                                        </asp:BoundField>
                                                                        <asp:BoundField DataField="InDate" HeaderText="In">
                                                                            <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" Width="75px" />
                                                                            <HeaderStyle Font-Bold="True" ForeColor="Black" VerticalAlign="Top" />
                                                                        </asp:BoundField>
                                                                        <asp:BoundField DataField="OutDate" HeaderText="Out">
                                                                            <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" Width="75px" />
                                                                            <HeaderStyle Font-Bold="True" ForeColor="Black" VerticalAlign="Top" />
                                                                        </asp:BoundField>
                                                                        <asp:BoundField DataField="Days_Since_In" HeaderText="Days Since In">
                                                                            <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" Width="100px" />
                                                                            <HeaderStyle Font-Bold="True" ForeColor="Black" VerticalAlign="Top" />
                                                                        </asp:BoundField>
                                                                        
                                                                        <asp:TemplateField>
                                                                            <ItemTemplate>
                                                                                <asp:Label ID="lbl_Days_To_Issue" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"InDate") %>' />
                                                                            </ItemTemplate>
                                                                        </asp:TemplateField>
                                                                        
                                                                        
                                                                        <asp:BoundField DataField="Days_To_Issue" HeaderText="Days To Issue">
                                                                            <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" Width="100px" />
                                                                            <HeaderStyle Font-Bold="True" ForeColor="Black" VerticalAlign="Top" />
                                                                        </asp:BoundField>
                                                                        
                                                                        
                                                                        
                                                                        
                                                                        <asp:BoundField DataField="RFD_Sent_Date" HeaderText="RFD Sent Date">
                                                                            <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" Width="100px" />
                                                                            <HeaderStyle Font-Bold="True" ForeColor="Black" VerticalAlign="Top" />
                                                                        </asp:BoundField>
                                                                        <asp:BoundField DataField="Days_Until_RFD" HeaderText="Days Until RFD Sent">
                                                                            <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" Width="100px" />
                                                                            <HeaderStyle Font-Bold="True" ForeColor="Black" VerticalAlign="Top" />
                                                                        </asp:BoundField>
                                                                        <asp:BoundField DataField="Team" HeaderText="Team" SortExpression="Team">
                                                                            <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" Width="100px" />
                                                                            <HeaderStyle Font-Bold="True" ForeColor="Black" VerticalAlign="Top" Wrap="False" />
                                                                        </asp:BoundField>
                                                                        <asp:BoundField DataField="Time" HeaderText="Time Spent">
                                                                            <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" Width="100px" />
                                                                            <HeaderStyle Font-Bold="True" ForeColor="Black" VerticalAlign="Top" Wrap="False" />
                                                                        </asp:BoundField>
                                                                        <asp:TemplateField>
                                                                            <ItemTemplate>
                                                                                <asp:ImageButton ID="Button3" runat="server" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"ID")%>'
                                                                                    CommandName="update" ImageUrl="images/edit2.jpg" />
                                                                            </ItemTemplate>
                                                                        </asp:TemplateField>
                                                                    </Columns>
                                                                    <RowStyle ForeColor="Black" Wrap="False" />
                                                                    <HeaderStyle BackColor="#B7DBFF" Font-Bold="True" ForeColor="Black" VerticalAlign="Top"
                                                                        Wrap="False" />
                                                                    <AlternatingRowStyle BackColor="#DBEDFF" />
                                                                </asp:GridView>



My VB.NET page:
CODE
'Begin Fill In/Outs Data Grid
            Dim SQLCommand As SqlDataAdapter
            Dim dataset_report As DataSet
            SQLCommand = New SqlDataAdapter("SELECT * FROM InOuts", db_connection)

            dataset_report = New DataSet()
            SQLCommand.Fill(dataset_report, "InOuts")

            inout_DataGrid.DataSource = dataset_report.Tables("InOuts").DefaultView
            inout_DataGrid.DataBind()
            'End Fill In/Outs Data Grid

User is offlineProfile CardPM
+Quote Post

baavgai
RE: GridView (DataBinder Perhaps?)
29 Jan, 2008 - 12:24 PM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,280



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(gymratz @ 29 Jan, 2008 - 03:07 PM) *

store the dates in SQL as a date/time it attaches 12:00:00am to all my fields


I seriously wouldn't worry about this, you can manipulate any output you need to. Besides, you'll want to do date math...

While you could add a dummy column to your dataset and manually manipulate it after the pull, it's slow. Better to let the database do it for you. Here's an example.

CODE

SQLCommand = New SqlDataAdapter("SELECT Quarter, InDate, OutDate, DATEDIFF (dd, InDate, OutDate) as Days_Since_In FROM InOuts", db_connection)


Hope this helps.

User is offlineProfile CardPM
+Quote Post

gymratz
RE: GridView (DataBinder Perhaps?)
29 Jan, 2008 - 12:55 PM
Post #3

D.I.C Head
**

Joined: 18 Oct, 2007
Posts: 86


My Contributions
That was perfect! I should be able to manipulate the rest of those on my own now :-).

Can you tell me how to trim the time off of the dates for displaying in my GridView? Is that something that would be done via SQL or something in vb?

(Edit)
One other thing I just realized haha.
Some of my fields are kind of mutually exclusive. For instance if there is something in the OutDate column then Days_Since_In should be skipped. Days_Since_In will only be used if there is no OutDate - indicating that it needs to be done.

Any ideas on this one? For some reason this one strikes me as a bit hard... lol

This post has been edited by gymratz: 29 Jan, 2008 - 01:06 PM
User is offlineProfile CardPM
+Quote Post

gymratz
RE: GridView (DataBinder Perhaps?)
29 Jan, 2008 - 01:48 PM
Post #4

D.I.C Head
**

Joined: 18 Oct, 2007
Posts: 86


My Contributions
If this can be done within the GridView control that would be the best because I would also like to have checks that if the result is greater than X amount of days it would change background color of that cell, etc.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: GridView (DataBinder Perhaps?)
29 Jan, 2008 - 03:16 PM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,280



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions

Can still make the database do the work. If you can get it all into a single select, then it's easy to get it into a DataTable.

SQL supports a case statement, so conditional logic is not that difficult. e.g.

CODE

SELECT Quarter, InDate, OutDate,
        (case when OutDate is null then DATEDIFF (dd, InDate, GetDate()) else null end) as Days_Since_In
    FROM InOuts



For formatting, color, etc, the GridView should be the tool of choice. Ask google and see how far you get.
http://www.google.com/search?q=asp.net+GridView+formatting

The GridView also supports conditional formatting, so you can turn cells on and off and ignore more complex SQL, if you like.

User is offlineProfile CardPM
+Quote Post

gymratz
RE: GridView (DataBinder Perhaps?)
30 Jan, 2008 - 10:04 AM
Post #6

D.I.C Head
**

Joined: 18 Oct, 2007
Posts: 86


My Contributions
Thank you thank you THANK YOU! smile.gif
I got my rows highlighting, I got specific cells highlighting, and I can hide certain cells when they aren't needed. Perfect!

You are the man, man!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 02:37PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month