DataGrid Problem

Last post 07-07-2009 3:41 PM by alessandro. 6 replies.

Sort Posts:

  • DataGrid Problem

    07-05-2009, 4:02 AM

    I am making application .net 1.1 I have added datagrid following is the code for aspx page

    <asp:datagrid id="dgServiceDetail" runat="server" width="100%" cssclass="normal_text" oneditcommand="dgServiceDetail_EditCommand"
                                                            oncancelcommand="dgServiceDetail_CancelCommand" onupdatecommand="dgServiceDetail_UpdateCommand"
                                                            autogeneratecolumns="False" borderwidth="1px" showfooter="True" gridlines="Horizontal" cellpadding="3"
                                                            backcolor="White" borderstyle="None" bordercolor="#E7E7FF" datakeyfield="id">
                                                            <footerstyle forecolor="#4A3C8C" backcolor="#B5C7DE"></footerstyle>
                                                            <selecteditemstyle font-bold="True" forecolor="#F7F7F7" backcolor="#738A9C"></selecteditemstyle>
                                                            <alternatingitemstyle backcolor="#F7F7F7"></alternatingitemstyle>
                                                            <itemstyle forecolor="#4A3C8C" backcolor="#E7E7FF"></itemstyle>
                                                            <headerstyle font-bold="True" forecolor="#F7F7F7" backcolor="#4A3C8C"></headerstyle>
                                                            <columns>
                                                                <asp:boundcolumn datafield="ServiceName" headertext="Service Name">
                                                                    <headerstyle width="250px"></headerstyle>
                                                                </asp:boundcolumn>
                                                                <asp:boundcolumn datafield="Description" headertext="Description">
                                                                    <headerstyle width="500px"></headerstyle>
                                                                </asp:boundcolumn>
                                                                <asp:templatecolumn headertext="No Of Passenger">
                                                                    <headerstyle width="100px"></headerstyle>
                                                                    <itemstyle horizontalalign="Center" verticalalign="Middle"></itemstyle>
                                                                    <itemtemplate>
                                                                        <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.NoOfPassenger") %>' id="lblNoOfPassenger"/>
                                                                    </itemtemplate>
                                                                    <edititemtemplate>
                                                                        <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.NoOfPassenger") %>' id="txNoOfPassengers"/>
                                                                    </edititemtemplate>
                                                                </asp:templatecolumn>
                                                                <asp:boundcolumn datafield="Price" headertext="Amount" dataformatstring="{0:0.000}">
                                                                    <itemstyle horizontalalign="Right" verticalalign="Middle"></itemstyle>
                                                                </asp:boundcolumn>
                                                                <asp:editcommandcolumn buttontype="LinkButton" updatetext="Update" canceltext="Cancel" edittext="Edit"></asp:editcommandcolumn>
                                                                <asp:buttoncolumn text="Delete" commandname="Delete"></asp:buttoncolumn>
                                                            </columns>
                                                            <pagerstyle horizontalalign="Right" forecolor="#4A3C8C" backcolor="#E7E7FF" mode="NumericPages"></pagerstyle>
                                                        </asp:datagrid>


    Now problem is that when i click on edit button first time it will not work but when i click second time it will make all field to textbox but in my code i have make only 1 field to textbox and next problem is it will get data just get blank textbox can anybody help me.

  • Re: DataGrid Problem

    07-05-2009, 8:26 AM
    • Member
      177 point Member
    • pavan496
    • Member since 06-18-2009, 8:44 PM
    • Hyderabad
    • Posts 59

    Please rebind the datagrid after any event.. editing, deleting and updating.

    and if you wrote gridview binding code in page load event, make sure the grid is binded even if the page is a postback. that means the gridview binding code should not be inside a

    If(!Page.IsPostBack) statement.

    This should solve all of your problems except the edititem template problem(all rows are being edited). I am not able to have a clear look at the gridview code you gave. Please use the insert code option to paste the code next time.

    Postback if you face anymore problems.

    Thanks

    PaVaN
  • Re: DataGrid Problem

    07-05-2009, 12:57 PM
    • Contributor
      6,768 point Contributor
    • alessandro
    • Member since 06-25-2002, 10:05 AM
    • Italy
    • Posts 1,103

    pavan496:

    Please rebind the datagrid after any event.. editing, deleting and updating.

    and if you wrote gridview binding code in page load event, make sure the grid is binded even if the page is a postback. that means the gridview binding code should not be inside a

    If(!Page.IsPostBack) statement.

    This should solve all of your problems except the edititem template problem(all rows are being edited). I am not able to have a clear look at the gridview code you gave. Please use the insert code option to paste the code next time.

    Postback if you face anymore problems.

    Thanks


    erhm. no. you are mistaken paven Foot in mouth or perhaps you have accidentally said the opposite by mistake since, in effect, rebinding early in page_load might give the poster the behavior he describes where the edit button requires 2 clicks to get enabled.

    milindsaraswala:

    when i click second time it will make all field to textbox but in my code i have make only 1 field to textbox and next problem is it will get data just get blank textbox can anybody help me.

    while you have only 1 textbox defined in the edittemplate ;

    The rest of the fields are boundfields. Boundfields are automatigically converted. If you do not want them to show in insert mode, then make the following change on the bound fields :

    <asp:boundcolumn datafield="ServiceName" headertext="Service Name" readonly="true">
        <headerstyle width="250px"></headerstyle>
    </asp:boundcolumn>
    <asp:boundcolumn datafield="Description" headertext="Description"  readonly="true">
        <headerstyle width="500px"></headerstyle>
    </asp:boundcolumn>

    note the two extra properties applied ot your boundcolumn :  readonly="true"


    as for a definitive answer on the double click required to enable your datagrid in editmode, provide code sample so we know what your doing Laughing

    Alessandro Zifiglio
    www.jiffycms.net - opensource HTML Editor for ASP.NET
    http://weblogs.asp.net/alessandro


    In the land of the blind, the man with one eye is king!:x
  • Re: DataGrid Problem

    07-06-2009, 4:26 AM

    I do the what both of u suggest but still not working. I am sending aspx and code bind code please just check it and Kindly help me.

    <asp:datagrid id="dgServiceDetail" runat="server" width="100%" cssclass="normal_text" oneditcommand="dgServiceDetail_EditCommand"
    														oncancelcommand="dgServiceDetail_CancelCommand" onupdatecommand="dgServiceDetail_UpdateCommand"
    														autogeneratecolumns="False" borderwidth="1px" showfooter="True" gridlines="Horizontal" cellpadding="3"
    														backcolor="White" borderstyle="None" bordercolor="#E7E7FF" datakeyfield="id">
    														<footerstyle forecolor="#4A3C8C" backcolor="#B5C7DE"></footerstyle>
    														<selecteditemstyle font-bold="True" forecolor="#F7F7F7" backcolor="#738A9C"></selecteditemstyle>
    														<alternatingitemstyle backcolor="#F7F7F7"></alternatingitemstyle>
    														<itemstyle forecolor="#4A3C8C" backcolor="#E7E7FF"></itemstyle>
    														<headerstyle font-bold="True" forecolor="#F7F7F7" backcolor="#4A3C8C"></headerstyle>
    														<columns>
    															<asp:boundcolumn datafield="ServiceName" readonly="True" headertext="Service Name">
    																<headerstyle width="250px"></headerstyle>
    															</asp:boundcolumn>
    															<asp:boundcolumn datafield="Description" readonly="True" headertext="Description">
    																<headerstyle width="500px"></headerstyle>
    															</asp:boundcolumn>
    															<asp:templatecolumn headertext="No Of Passenger">
    																<headerstyle width="100px"></headerstyle>
    																<itemstyle horizontalalign="Center" verticalalign="Middle"></itemstyle>
    																<itemtemplate>
    																	<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.NoOfPassenger") %>' id="lblNoOfPassenger"/>
    																</itemtemplate>
    																<edititemtemplate>
    																	<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.NoOfPassenger") %>' id="txNoOfPassengers"/>
    																</edititemtemplate>
    															</asp:templatecolumn>
    															<asp:boundcolumn datafield="Price" readonly="True" headertext="Amount" dataformatstring="{0:0.000}">
    																<itemstyle horizontalalign="Right" verticalalign="Middle"></itemstyle>
    															</asp:boundcolumn>
    															<asp:editcommandcolumn buttontype="LinkButton" updatetext="Update" canceltext="Cancel" edittext="Edit"/>
    															<asp:buttoncolumn text="Delete" commandname="Delete"/>
    														</columns>
    														<pagerstyle horizontalalign="Right" forecolor="#4A3C8C" backcolor="#E7E7FF" mode="NumericPages"></pagerstyle>
    													</asp:datagrid>

    and this is the VB code (code behind code)

      Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
    
        If Not Page.IsPostBack Then BindService()
    
      End Sub
    
    
    Protected Sub BindService()
        Dim cn As New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("connstring"))
        Dim strSelect As String = "Select ServiceID,ServiceName From Service"
        Dim dtadapter As New SqlDataAdapter(strSelect, cn)
    
        cn.Open()
    
        Dim ds As New DataSet
        dtadapter.Fill(ds)
        dpSelectService.DataSource = ds
        dpSelectService.DataTextField = "ServiceName"
        dpSelectService.DataValueField = "ServiceID"
        dpSelectService.DataBind()
    
        dtadapter.Dispose()
        ds.Dispose()
        cn.Close()
    
      End Sub
    
    Private Sub dgServiceDetail_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgServiceDetail.ItemDataBound
        Select Case e.Item.ItemType
          Case ListItemType.Item, ListItemType.AlternatingItem
            interimTotal += CType(e.Item.Cells(3).Text, Double)
          Case ListItemType.Footer
            e.Item.Cells.Clear()
            Dim oCell As New TableCell
            oCell.ColumnSpan = 3
            oCell.Text = "Your Total Shopping Amount is"
            oCell.HorizontalAlign = HorizontalAlign.Right
            e.Item.Cells.Add(oCell)
            Dim oCell1 As New TableCell
            oCell1.Text = Format(interimTotal, "0.000")
            oCell1.HorizontalAlign = HorizontalAlign.Right
            oCell1.ForeColor = Color.Red
            e.Item.Cells.Add(oCell1)
            Dim oCell2 As New TableCell
            oCell2.ColumnSpan = 2
            oCell2.Text = String.Empty
            e.Item.Cells.Add(oCell2)
        End Select
      End Sub
    
      Public Sub dgServiceDetail_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgServiceDetail.EditCommand
        dgServiceDetail.EditItemIndex = dgServiceDetail.DataKeys(e.Item.ItemIndex)
      End Sub
    
      Public Sub dgServiceDetail_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgServiceDetail.UpdateCommand
        Dim ServiceName As String = CType(e.Item.FindControl("txServiceName"), TextBox).Text
        Dim NoOfPassenger As String = CType(e.Item.FindControl("txNoOfPassengers"), TextBox).Text
        'Dim id as Integer=ctype(e.Item.
    
        Dim cn As New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("connstring"))
        Dim cmd As New SqlCommand("Update Service Set NoOfPassengers='" & NoOfPassenger & "' Where id=" & dgServiceDetail.DataKeys(e.Item.ItemIndex), cn)
        cn.Open()
        cmd.ExecuteNonQuery()
    
        cn.Close()
        cmd.Dispose()
      End Sub
    
      Public Sub dgServiceDetail_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgServiceDetail.CancelCommand
        dgServiceDetail.EditItemIndex = -1
      End Sub




  • Re: DataGrid Problem

    07-06-2009, 5:03 AM
    • Contributor
      6,768 point Contributor
    • alessandro
    • Member since 06-25-2002, 10:05 AM
    • Italy
    • Posts 1,103

    Firstly, I don't see your code that passes data to your datagrid's datasource and databinds it.  Just make sure it's wrapped inside an "If not Page.IsPostBack then" block, same way you did with BindService() call in page_load :-)

    and make the following changes :

    Public Sub dgServiceDetail_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgServiceDetail.EditCommand  
        'IMPORTANT: pass the itemindex of the current ROW, not datakey!
        dgServiceDetail.EditItemIndex = e.Item.ItemIndex
        'IMPORTANT pass the datasource :
        dgServiceDetails.DataSource = pass_your_datasource_here_again
        'IMPORTANT: then call databind on it
        dgServiceDetails.DataBind()
    End Sub

    also with your cancelCommand, do the same as above :

     Public Sub dgServiceDetail_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgServiceDetail.CancelCommand
        dgServiceDetail.EditItemIndex = -1
        'IMPORTANT pass the datasource :
        dgServiceDetails.DataSource = pass_your_datasource_here_again
        'IMPORTANT: then call databind on it
        dgServiceDetails.DataBind()
      End Sub

    As for the readonly change on your bound columns as i suggested in the previous post are already in place, so on that end you are good.

    This should practically fix the issues you described. Unless I missed something. Report back if it's still not working while also stating "exactly" what is still not working Surprised

    Alessandro Zifiglio
    www.jiffycms.net - opensource HTML Editor for ASP.NET
    http://weblogs.asp.net/alessandro


    In the land of the blind, the man with one eye is king!:x
  • Re: DataGrid Problem

    07-07-2009, 3:07 AM

    hi alessandro,


    I do what you say to me. But now it is not going to edit mode when click on edit button, before it was going with two time click but now not at all. Following is the chnages in code behin

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then BindService()
    End Sub
    
    Protected Sub BindService()
        Dim cn As New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("connstring"))
        Dim strSelect As String = "Select ServiceID,ServiceName From Service"
        Dim dtadapter As New SqlDataAdapter(strSelect, cn)
    
        cn.Open()
    
        Dim ds As New DataSet
        dtadapter.Fill(ds)
        dpSelectService.DataSource = ds
        dpSelectService.DataTextField = "ServiceName"
        dpSelectService.DataValueField = "ServiceID"
        dpSelectService.DataBind()
    
        dtadapter.Dispose()
        ds.Dispose()
        cn.Close()
    
      End Sub
    
      Public Sub dgServiceDetail_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgServiceDetail.EditCommand
        dgServiceDetail.EditItemIndex = dgServiceDetail.DataKeys(e.Item.ItemIndex)
    
        BindService()
      End Sub
    
      Public Sub dgServiceDetail_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgServiceDetail.UpdateCommand
        Dim ServiceName As String = CType(e.Item.FindControl("txServiceName"), TextBox).Text
        Dim NoOfPassenger As String = CType(e.Item.FindControl("txNoOfPassengers"), TextBox).Text
        'Dim id as Integer=ctype(e.Item.
    
        Dim cn As New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("connstring"))
        Dim cmd As New SqlCommand("Update Service Set NoOfPassengers='" & NoOfPassenger & "' Where id=" & dgServiceDetail.DataKeys(e.Item.ItemIndex), cn)
        cn.Open()
        cmd.ExecuteNonQuery()
    
        cn.Close()
        cmd.Dispose()
      End Sub
    
      Public Sub dgServiceDetail_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgServiceDetail.CancelCommand
        dgServiceDetail.EditItemIndex = -1
    
        BindService()
      End Sub


    Please help me.

  • Re: DataGrid Problem

    07-07-2009, 3:41 PM
    Answer
    • Contributor
      6,768 point Contributor
    • alessandro
    • Member since 06-25-2002, 10:05 AM
    • Italy
    • Posts 1,103

     I have asked you to make some corrections. Look at my previous post. I don't see any of the corrections in the new code you pasted. Seems logical that the problem persists.

    Firstly, why do you keep calling BindService() ? This is simply binding a dropdownlist. YOU MUST DO THIS FOR YOUR *DATAGRID* not for your dropdownlist Yell.

    Where is the code that passes data to your datagrid Surprised

    Alessandro Zifiglio
    www.jiffycms.net - opensource HTML Editor for ASP.NET
    http://weblogs.asp.net/alessandro


    In the land of the blind, the man with one eye is king!:x
Page 1 of 1 (7 items)