[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

10/01/2006 at 11:47PM PDT, ID: 22009423
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6

datacombo in datagrid

Asked by vblovebug in Programming for ASP.NET

Tags: datacombo, add, boundcolumn, datagrid_update

I have a webpage which retrieves data from a table and display it in a datagrid. This datagrid has edit, cancel, update and add new links. When i click on the edit or add new link, there appears a textbox in the fields. If i enter a value and update it works fine... but I dont want textbox there , instead I want a dropdown menu which should fetch data from another table. so that the user dont have to type but just select it...BUt these dropdown should only appear when I click on edit or new . Can it be done????

How can I do that...
My code is like this...


<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

    ' TODO: update the ConnectionString and Command values for your application
   
    Dim ConnectionString As String = "server=ser;uid=sa;pwd=sa;database=data;"

    Dim SelectCommand As String = "select FullName, GroupName from tblusers inner join tblwebaccess on tblwebaccess.id_user=tblusers.id_user inner join tblReports_AutoGen_GroupTypes on tblwebaccess.ID_reportgroup =tblReports_AutoGen_GroupTypes.ID_reportgroup order by fullname"
    Dim isEditing As Boolean = False
   
    Sub Page_Load(Sender As Object, E As EventArgs)
   
        If Not Page.IsPostBack Then
   
            ' Databind the data grid on the first request only
            ' (on postback, bind only in editing, paging and sorting commands)
   
            BindGrid()
   
        End If
   
    End Sub
   
    ' ---------------------------------------------------------------
    '
    ' DataGrid Commands: Page, Sort, Edit, Update, Cancel, Delete
    '
   
    Sub DataGrid_ItemCommand(Sender As Object, E As DataGridCommandEventArgs)
   
        ' this event fires prior to all of the other commands
        ' use it to provide a more graceful transition out of edit mode
   
        CheckIsEditing(e.CommandName)
   
    End Sub
   
    Sub CheckIsEditing(commandName As String)
   
        If DataGrid1.EditItemIndex <> -1 Then
   
            ' we are currently editing a row
            If commandName <> "Cancel" And commandName <> "Update" Then
   
                ' user's edit changes (If any) will not be committed
                Message.Text = "Your changes have not been saved yet.  Please press update to save your changes, or cancel to discard your changes, before selecting another item."
                isEditing = True
   
            End If
   
        End If
   
    End Sub
   
    Sub DataGrid_Edit(Sender As Object, E As DataGridCommandEventArgs)
   
        ' turn on editing for the selected row
   
        If Not isEditing Then
   
            DataGrid1.EditItemIndex = e.Item.ItemIndex
            BindGrid()
   
        End If
   
    End Sub
   
    Sub DataGrid_Update(Sender As Object, E As DataGridCommandEventArgs)
   
        ' update the database with the new values
   
        ' get the edit text boxes
        'Dim id As String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
        Dim fullname As String = CType(e.Item.Cells(0).Controls(0), TextBox).Text
        Dim groupname As String = CType(e.Item.Cells(1).Controls(0), TextBox).Text
   
        ' TODO: update the Command value for your application
   
   
        Dim myConnection As New SqlConnection(ConnectionString)
        Dim UpdateCommand As SqlCommand = new SqlCommand()
        UpdateCommand.Connection = myConnection
   
        If AddingNew = True Then
            UpdateCommand.CommandText = "insert into tblwebaccess (id_user,id_reportgroup) select id_user,ID_reportgroup from tblusers,tblReports_AutoGen_GroupTypes where tblusers.fullname like '" & fullname &"%' and tblReports_AutoGen_GroupTypes.groupname like '" & groupname & "%'"
        Else
            'UpdateCommand.CommandText = "UPDATE authors SET au_lname = @au_lname, au_fname = @au_fname WHERE au_id = @au_id"
        End If
   
        'UpdateCommand.Parameters.Add("@au_id", SqlDbType.VarChar, 11).Value = id
        'UpdateCommand.Parameters.Add("@au_lname", SqlDbType.VarChar, 40).Value = lname
       ' UpdateCommand.Parameters.Add("@au_fname", SqlDbType.VarChar, 20).Value = fname
   
        ' execute the command
        Try
            myConnection.Open()
            UpdateCommand.ExecuteNonQuery()
   
        Catch ex1 as SQLException
             Message.Text="Record already present"
   
        Catch ex as Exception
            Message.Text = ex.ToString()
   
   
   
        Finally
            myConnection.Close()
   
        End Try
   
        ' Resort the grid for new records
        If AddingNew = True Then
            DataGrid1.CurrentPageIndex = 0
            AddingNew = false
        End If
   
        ' rebind the grid
        DataGrid1.EditItemIndex = -1
        BindGrid()
   
    End Sub
   
    Sub DataGrid_Cancel(Sender As Object, E As DataGridCommandEventArgs)
   
        ' cancel editing
   
        DataGrid1.EditItemIndex = -1
        BindGrid()
   
        AddingNew = False
   
    End Sub
   
    Sub DataGrid_Delete(Sender As Object, E As DataGridCommandEventArgs)
   
        ' delete the selected row
   
        If Not isEditing Then
   
            ' the key value for this row is in the DataKeys collection
            'Dim keyValue As String = CStr(DataGrid1.DataKeys(e.Item.ItemIndex))
            'response.write(e.Item.ItemIndex)
               'response.write(e.item.cells(3).text)
   
            ' TODO: update the Command value for your application
            Dim myConnection As New SqlConnection(ConnectionString)
            'Dim DeleteCommand As New SqlCommand("DELETE from tblwebaccess where id=" & keyValue , myConnection)
            Dim DeleteCommand As New SqlCommand("delete from tblwebaccess where id_user=(select id_user from tblusers where fullname like '" & e.item.cells(0).text &"') and id_ReportGroup=(select id_reportgroup from tblReports_AutoGen_GroupTypes where groupname like '" & e.item.cells(1).text & "')" , myConnection)
                        ' execute the command
            myConnection.Open()
            DeleteCommand.ExecuteNonQuery()
            myConnection.Close()
   
            ' rebind the grid
            DataGrid1.CurrentPageIndex = 0
            DataGrid1.EditItemIndex = -1
            BindGrid()
   
        End If
   
    End Sub
   
    Sub DataGrid_Page(Sender As Object, E As DataGridPageChangedEventArgs)
   
        ' display a new page of data
   
        If Not isEditing Then
   
            DataGrid1.EditItemIndex = -1
            DataGrid1.CurrentPageIndex = e.NewPageIndex
            BindGrid()
   
        End If
   
    End Sub
   
    Sub AddNew_Click(Sender As Object, E As EventArgs)
   
        ' add a new row to the end of the data, and set editing mode 'on'
   
        CheckIsEditing("")
   
        If Not isEditing = True Then
   
            ' set the flag so we know to do an insert at Update time
            AddingNew = True
   
            ' add new row to the end of the dataset after binding
   
            ' first get the data
            Dim myConnection As New SqlConnection(ConnectionString)
            Dim myCommand As New SqlDataAdapter(SelectCommand, myConnection)
   
            Dim ds As New DataSet()
            myCommand.Fill(ds)
   
            ' add a new blank row to the end of the data
            Dim rowValues As Object() = {"", ""}
            ds.Tables(0).Rows.Add(rowValues)
   
            ' figure out the EditItemIndex, last record on last page
            Dim recordCount As Integer = ds.Tables(0).Rows.Count
   
            If recordCount > 1 Then
   
                recordCount -= 1
                DataGrid1.CurrentPageIndex = recordCount \ DataGrid1.PageSize
                DataGrid1.EditItemIndex = recordCount Mod DataGrid1.PageSize
   
            End If
   
            ' databind
            DataGrid1.DataSource = ds
            DataGrid1.DataBind()
   
        End If
   
   
    End Sub
   
    ' ---------------------------------------------------------------
    '
    ' Helpers Methods:
    '
   
    ' property to keep track of whether we are adding a new record,
    ' and save it in viewstate between postbacks
   
    Property AddingNew() As Boolean
   
        Get
            Dim o As Object = ViewState("AddingNew")
            If o Is Nothing Then
                Return False
            End If
            Return CBool(o)
        End Get
   
        Set(ByVal Value As Boolean)
            ViewState("AddingNew") = Value
        End Set
   
    End Property
   
    Sub BindGrid()
   
        Dim myConnection As New SqlConnection(ConnectionString)
        Dim myCommand As New SqlDataAdapter(SelectCommand, myConnection)
   
        Dim ds As New DataSet()
        myCommand.Fill(ds)
   
        DataGrid1.DataSource = ds
        DataGrid1.DataBind()
        'DataGrid1.columns(0).visible=false
    End Sub

</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
    <h2>Web Access Control
    </h2>
    <hr size="1" />
    <form runat="server">
        <asp:datagrid id="DataGrid1" runat="server" width="80%" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" ForeColor="Black" OnPageIndexChanged="DataGrid_Page" PageSize="6" AllowPaging="true" OnDeleteCommand="DataGrid_Delete" OnCancelCommand="DataGrid_Cancel" OnUpdateCommand="DataGrid_Update" OnEditCommand="DataGrid_Edit" OnItemCommand="DataGrid_ItemCommand" DataKeyField="" AutoGenerateColumns="False" AllowSorting="True">
            <FooterStyle backcolor="#C6C3C6"></FooterStyle>
            <PagerStyle mode="NumericPages" backcolor="#C6C3C6" font-size="Smaller" horizontalalign="Right"></PagerStyle>
            <AlternatingItemStyle backcolor="PaleGoldenrod"></AlternatingItemStyle>
            <ItemStyle backcolor="Beige"></ItemStyle>
            <HeaderStyle backcolor="#4A3C8C" forecolor="White" font-bold="True"></HeaderStyle>
            <Columns>
                <asp:BoundColumn DataField="FullName" HeaderText="Full Name"></asp:BoundColumn>
                <asp:BoundColumn DataField="GroupName" HeaderText="Group Name"></asp:BoundColumn>

                <asp:EditCommandColumn CancelText="Cancel" UpdateText="Update" EditText="Edit">
                    <ItemStyle width="10%" font-size="Smaller"></ItemStyle>
                </asp:EditCommandColumn>
                <asp:ButtonColumn CommandName="Delete" Text="Delete">
                    <ItemStyle width="10%" font-size="Smaller"></ItemStyle>
                </asp:ButtonColumn>
            </Columns>
        </asp:datagrid>
        <br />
        <asp:LinkButton id="LinkButton1" onclick="AddNew_Click" runat="server" Font-Size="smaller" Text="Add new item"></asp:LinkButton>
        <br />
        <br />
        <asp:Label id="Message" runat="server" width="80%" enableviewstate="false" forecolor="red"></asp:Label>
    </form>
</body>
</html>
[+][-]10/02/06 02:07 AM, ID: 17641700

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Programming for ASP.NET
Tags: datacombo, add, boundcolumn, datagrid_update
Sign Up Now!
Solution Provided By: daniel_balla
Participating Experts: 1
Solution Grade: A
 
 
[+][-]12/12/06 05:54 AM, ID: 18122286

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]12/16/06 05:41 PM, ID: 18153437

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20090824-EE-VQP-74