Link to home
Start Free TrialLog in
Avatar of arunatata
arunatata

asked on

What type of column to use?

Hi,

I have a datagrid with data columns "Date" and "status" for a particular document. I'd like to include a third button column called "reserve". I want this column to show up only if the status for the document is set to "Available". When the user clicks the button in a row, I don't want the datagrid to become editable. Instead all it should do is update the database and set the status for that particular row to "Checked out" and refresh the datagrid.

My problem is that I am not sure what kind of column i should use for this. A template column or a button column? Also how can I do the above using this column?

Thanks,
Aruna
ASKER CERTIFIED SOLUTION
Avatar of netcool
netcool
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of arunatata
arunatata

ASKER

Hi Netcool,

My datagrid has autogenerated columns and I added a button column in addition to those. The datagrid thus now has 3 columns

ButtonCol                  Date                      Status
Reserve                     4/17/05                  Available
Reserve                     4/18/05                  Available
                                  4/19/05                Unavailable

The code for the datagrid itself is a whole different story. I created a datatable, flipped the columns and then bound them to the datagrid. I then programatically added the buttoncolumn using the following code:

            Dim BtnCol As New ButtonColumn
            With BtnCol
                .Text = "Reserve "
                .CommandName = "DG_Update(" & DocName & "," & ReservedDate & ")"
                .HeaderText = "Action"
            End With
            DG.Columns.Add(BtnCol)
            DG.DataBind()

All the above code created the datagrid shown above. As you can see, the commandname for the ButtonColumn has two parameters - DocName and ReservedDate.

Now whenever someone clicks on "Reserve" in the datagrid, I want it to go ahead and update the database with the status for that date set to "Unavailable". It should then update the datagrid with the fresh data.

My problem here is I don't know how to get the parameters from this code to the other sub "DG_Update" where the update function is being called.  The itemcommand event needs DocName and ReservedDate as inputs. How do I pass these to it? And is the method I am following above correct?

Thanks,
A.
Actually moved a little further on this - I used viewstate to store the values of DocName and ReservedDate and am calling them from the same when the itemcommand event fires. However the itemcommand event is NOT firing. Please help!!

The structure of the page is as below:

It has two listboxes and a button. The button_click event take the values of the listboxes and uses them to query the database and print out a datagrid.

Sub Page_load
        If Not Page.IsPostBack Then
'Bind data from a table to the listbox
            Dim mainConnString As String = "Data Source=biginfo;Initial Catalog=aruna;User ID=sa"
            Dim myConn As New SqlConnection(mainConnString)
            Dim QueryString As String = "SELECT DocName FROM DocReserveDetail a INNER JOIN DocDetail b ON a.DocID = b.DocID"
            Dim myCmd As New SqlCommand(QueryString, myConn)
            myConn.Open()
            Dim myDR As SqlDataReader
            myDR = myCmd.ExecuteReader
            ListDocs.DataSource = myDR
            ListDocs.DataTextField = "DocName"

            ListDocs.DataBind()

            myDR.Close()
        myConn.Close()
 End If
End sub

Sub button_click
if page.ispostback then
'get values for DocName and Date
'Get dataset from getDataset() and bind to the datagrid
'add buttoncolumn to the datagrid
            Dim myDV As DataView = getDataset(DocName, newDate).Tables(0).DefaultView

            DG.DataSource = myDV
            Dim BtnCol As New ButtonColumn
            With BtnCol
                .Text = "Reserve "
                .CommandName = "DG_Update"
                .HeaderText = "Action"
            End With
            DG.Columns.Add(BtnCol)
            DG.Attributes.Add("OnitemCommand", "DG_Update")

            DG.DataBind()
            ViewState("DocName") = DocName
            ViewState("newDate") = newDate
end if
end sub

Sub getDataset()
'Get values from database and bind
end sub

Sub DG_Update(byval sender as object, byval e as datagridcommandeventargs) handles dg.itemcommand
'THIS SUB IS NOT FIRING

Dim testername as string = convert.tostring(Viewstate("Testername"))
Dim newDate as string = convert.tostring(ViewState("newDate"))
end sub