Results 1 to 8 of 8

Thread: Datagrid visible ? Argh

  1. #1

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Datagrid visible ? Argh

    Guys,

    Why, when I add a datagrid control to my screen, can I not get it to show at runtime ???

    In a VB.Net windows app, I would set the datasource at runtime to a datatable held in memory, and it would all be cool.

    Now in an ASP app I can't even get this datagrid to show ! I just need to show 2 columns, a name and a status. Is the datagrid the right way to go ?

    Thanks
    Bob

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Datagrid visible ? Argh

    Not sure if the DataGrid is overkill - it depends what you are going to do to the data. For example for just displaying records in a database a DataGrid isn't the most efficient control to use. Give us a few more details.

    Also post some code for how you are creating the DataTable and DataBinding the DataGrid.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  3. #3

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: Datagrid visible ? Argh

    Thanks for the feedback,

    Agreed, I think the datatable is overkill, but this is the only method I have used before in my forms app.

    I just need to display the 2 columns selected, readonly, but I would also like the ability to change the header text and also replace the status returned with a string. Not sure if I should do this at runtime, or in my SQL view ? I don't know how to do either !

    Status could be an int, 0 - 9 inclusive. I want to replace this with . .

    0 = "Not started"
    1 = 13%
    2 = 25%
    3 = 38%
    ...
    8 = 100%
    9 = "Submitted"

    This is my code to bind and display my datatable, which I have now managed to get showing at runtime, and my db class that builds the datatable.

    TIA

    Bob

    VB Code:
    1. Dim usersdt As New DataTable
    2.         db.QbyID.QIDFill(usersdt, CInt(Questionnaires.SelectedItem.Value))
    3.  
    4.         DataGrid1.DataSource = New DataView(usersdt)
    5.         DataGrid1.DataBind()
    6.         DataGrid1.Visible = True
    7.  
    8.         usersdt.Dispose()
    9.         usersdt = Nothing

    VB Code:
    1. Class QbyID
    2.         Private Shared mda As SqlDataAdapter
    3.         Friend Shared Function QIDFill(ByVal dt As DataTable, ByVal QID As Integer) As Integer
    4.             QIDDataAdapter.SelectCommand.Parameters(0).Value = QID
    5.             Return QIDDataAdapter.Fill(dt)
    6.         End Function
    7.         Private Shared Function QIDDataAdapter() As SqlDataAdapter
    8.             If Not mda Is Nothing Then Return mda
    9.             mda = New SqlDataAdapter
    10.             With mda
    11.                 .SelectCommand = New SqlCommand
    12.                 With .SelectCommand
    13.                     .Connection = cnn
    14.                     .CommandText = "SELECT Staffname, QLStatus FROM vwQuestionnaireLine WHERE Questionnaire = @QID"
    15.                     .Parameters.Add("@QID", SqlDbType.Int)
    16.                 End With
    17.                 AddHandler .FillError, AddressOf Fill_Error
    18.                 AddHandler .RowUpdated, New SqlRowUpdatedEventHandler(AddressOf DataAdapter_OnRowUpdated)
    19.             End With
    20.             Return mda
    21.         End Function

  4. #4
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Datagrid visible ? Argh

    Right so this is a read only table containing two columns one for name and the other for status.

    Personally I would just use a DataReader and a Repeater to do what you want.

    Code:
    <asp:Repeater id="rptItems" runat="server">
    	<HeaderTemplate>
    		<table>
    			<tr>
    				<th>Name</th>
    				<th>Status</th>
    			</tr>
    	<HeaderTemplate>
    	<ItemTemplate>
    		<tr>
    			<td><%# DataBinder.Eval(Container.DataItem, "Staffname") %></td>
    			<td><%# displayStatus(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "QLStatus"))) %></td>
    		</tr>
    	</ItemTemplate>
    	<FooterTemplate>
    		</table>
    	</FooterTemplate>
    </asp:Repeater>
    Then in the code behind you would populate the key/value pairs for the status and then use this in the displayStatus function used above to display the status string.

    Code:
    string displayStatus(int statusID) {
    	Hashtable _status = new HashTable();
    	_status.Add(0, "Not started");
    	_status.Add(1, "13%");
    	...
    	_status.Add(8, "100%");
    	_status.Add(9, "Submitted");
    	
    	return _status[statusID];
    }
    I'd then create a method to grab the data and DataBind to the repeater - this could be called anywhere but generally I'd do it in Page_Load but it looks like to me this is called when the Questionnaires control (?Dropdownlist?) changes. Hence you pass the selectedvalue into the DataBind method.

    Code:
    void rptItems_DataBind(int QID) {
    	SqlConnection dbConn = new SqlConnection("Enter connection string here");
    	SqlCommand cmdSelect = new SqlCommand("SELECT Staffname, QLStatus FROM vwQuestionnaireLine WHERE Questionnaire = @QID", dbConn);
            cmdSelect.Parameters.Add("@QID", QID);
    	
    	dbConn.Open();
    	SqlDataReader dtrItems = cmdSelect.ExecuteReader();
    	
    	rptItems.DataSource = dtrItems;
    	rptItems.DataBind();
    
    	dtrItems.Close();
    	dbConn.Close();
    }
    Sorry it's in C# but I can read VB.NET fine but writing it is not so easy!

    Let me know if I've misunderstood or you have any other questions.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  5. #5

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: Datagrid visible ? Argh

    Thanks DJ,

    I now have these 3 lots of code, I made some changes to the html to make it build, but it doesn't display well, I just get . .

    Name Status
    BLUESTONE\matt.adam BLUESTONE\neil.bailey BLUESTONE\susan.ball BLUESTONE\david.barker BLUESTONE\peter.blackburn BLUESTONE\mark.brown BLUESTONE\jason.jones

    . . .. rather than a new row for each user.

    I have posted all code, my html, function and method.

    Thanks again . .
    Bob

    Code:
    <asp:Repeater id="rptitems" runat="server">
    					<ItemTemplate>
    						<tr>
    							<td><%# DataBinder.Eval(Container.DataItem, "Staffname") %></td>
    							<td><%# displayStatus(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "QLStatus"))) %></td>
    						</tr>
    					</ItemTemplate>
    					<HeaderTemplate>
    						<table>
    							<tr>
    								<th>
    									Name</th>
    								<th>
    									Status</th>
    							</tr>
    						</table>
    					</HeaderTemplate>
    					<FooterTemplate>
    					</FooterTemplate>
    				</asp:Repeater>
    VB Code:
    1. Public Function displayStatus(ByVal statusID As Integer)
    2.         Dim _status As New Hashtable
    3.         _status.Add(0, "Not Started")
    4.         _status.Add(1, "13% Complete")
    5.         _status.Add(2, "25% Complete")
    6.         _status.Add(3, "38% Complete")
    7.         _status.Add(4, "50% Complete")
    8.         _status.Add(5, "63% Complete")
    9.         _status.Add(6, "75% Complete")
    10.         _status.Add(7, "88% Complete")
    11.         _status.Add(8, "100% Complete")
    12.         _status.Add(9, "Submitted")
    13.     End Function
    14.  
    15.     Public Sub rptItems_Databing(ByVal QID As Integer)
    16.         Dim cnn As New SqlConnection("Data Source=BLS5TEST2;Initial Catalog=HPT;User Id=HPT;Password=kibby1;")
    17.         Dim cmd As New SqlCommand("SELECT Staffname, QLStatus FROM vwQuestionnaireLine WHERE Questionnaire = '" & QID & "'", cnn)
    18.  
    19.         cnn.Open()
    20.  
    21.         Dim dr As SqlDataReader = cmd.ExecuteReader()
    22.         rptitems.DataSource = dr
    23.         rptitems.DataBind()
    24.  
    25.         cnn.Close()
    26.  
    27.     End Sub

  6. #6
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Datagrid visible ? Argh

    The </table> should be in the FooterTemplate not the HeaderTemplate.

    Try that.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  7. #7

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: Datagrid visible ? Argh

    Thanks mate, got that one.

    The status doesn't show tho ?

    Cheers
    Bob

  8. #8
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Datagrid visible ? Argh

    Because you aren't returning a value from the displayStatus function - it should return the value for the statusID entered.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width