Results 1 to 7 of 7

Thread: Help with gridview

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2019
    Posts
    8

    Help with gridview

    Newbie here, i have an asp.net web page with a gridview that has multiple columns with one of them being a 'Comments' column.
    I have an edit button in the gridview table as well, everything works great, even the editing. My question is, is it possible to be able to Edit the 'Comments' column at selected row and have the edited data attached to the existing data and use a different font to that added text? Any help would be appreciated.

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Help with gridview

    Of course its possible, but ...

    can you post your current code so we can see what your doing and help correct it !
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2019
    Posts
    8

    Re: Help with gridview

    Here is the asp.net code for the grid
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" style="width:100%;overflow:auto" DataKeyNames="id" >
    <Columns>
    <asp:BoundField DataField="id" HeaderText="Id" ControlStyle-Width="1px">
    <ControlStyle Width="1px"></ControlStyle>

    <HeaderStyle BorderStyle="None" ForeColor="White" Width="1px" />
    <ItemStyle BorderStyle="None" ForeColor="White" Width="1px" />
    </asp:BoundField>
    <asp:BoundField DataField="Region">
    <ItemStyle ForeColor="#E6FFE6" HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" Wrap="False" />
    </asp:BoundField>
    <asp:TemplateField HeaderText="Employee" ControlStyle-Width="100px">
    <ItemTemplate>
    <asp:HyperLink ID="HyperLink2" runat="server" ControlStyle-Width="75px" Text='<%# Eval("name") %>' NavigateUrl='<%#String.Format("~/Admin/DailyByName.aspx?Id={0}&Name={1}", HttpUtility.UrlEncode(Eval("UserId").ToString()), HttpUtility.UrlEncode(Eval("name").ToString())) %>'></asp:HyperLink>
    </ItemTemplate>

    <ControlStyle Width="100px"></ControlStyle>

    <ItemStyle BorderColor="Black" ForeColor="Blue" />
    </asp:TemplateField>
    <asp:BoundField DataField="Date" DataFormatString="{0}" HeaderText="Date" ControlStyle-Width="100px" SortExpression="Date">
    <ControlStyle Width="100px"></ControlStyle>

    <HeaderStyle Height="50px" Width="140px" HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle Height="50px" Width="140px" HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:BoundField>
    <asp:TemplateField HeaderText="Ticket #">
    <ItemTemplate>
    <asp:HyperLink ID="HyperLink1" runat="server" ControlStyle-Width="75px" Text='<%# Eval("InvoiceNumber") %>' NavigateUrl='<%# Eval("InvoiceNumber", "/Admin/Invoice.aspx?Id={0}") %>' ></asp:HyperLink>
    </ItemTemplate>
    <ItemStyle BorderColor="Black" ForeColor="Blue" />
    <EditItemTemplate>
    <asp:TextBox ID="hyperlinkvalue" Text='<%#Eval("InvoiceNumber")%>' Width="75" runat="server" />
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="FieldHours" HeaderText="Field Hrs" SortExpression="FieldHours" ControlStyle-Width="75px" DataFormatString="{0:#.#;;&quot;&quot;}" >
    <ControlStyle Width="75px"></ControlStyle>

    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:BoundField>
    <asp:BoundField DataField="Standby" HeaderText="Standby" SortExpression="Standby" ControlStyle-Width="75px" DataFormatString="{0:#.#;;&quot;&quot;}" >
    <ControlStyle Width="75px"></ControlStyle>

    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:BoundField>
    <asp:BoundField DataField="Shop" HeaderText="Shop" SortExpression="Shop" ControlStyle-Width="75px" DataFormatString="{0:#.#;;&quot;&quot;}" >
    <ControlStyle Width="75px"></ControlStyle>

    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:BoundField>
    <asp:BoundField DataField="Travel" DataFormatString="{0:#.#;;&quot;&quot;}" ControlStyle-Width="75px" HeaderText="Travel" >
    <ControlStyle Width="75px"></ControlStyle>

    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:BoundField>
    <asp:BoundField DataField="SubPay" HeaderText="SubPay" SortExpression="SubPay" ControlStyle-Width="75px" DataFormatString="{0:#.#;;&quot;&quot;}" >
    <ControlStyle Width="75px"></ControlStyle>

    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:BoundField>
    <asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" ControlStyle-Width="375px" >
    <ControlStyle Width="375px"></ControlStyle>

    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />
    </asp:BoundField>
    <asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID" Visible="False" >
    </asp:BoundField>
    <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
    <asp:TemplateField HeaderText="Approved">
    <ItemTemplate>
    <asp:CheckBox ID="Approved" runat="server" Checked='<%# Bind("Approved") %>' Enabled="true"/>
    </ItemTemplate>
    <HeaderStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Warning">
    <ItemTemplate>
    <asp:CheckBox ID="Warning" runat="server" Checked='<%# Bind("Warning") %>' Enabled="true"/>
    </ItemTemplate>
    <HeaderStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:TemplateField>
    <asp:BoundField DataField="Salary">
    <ItemStyle HorizontalAlign="Left" Width="1px" ForeColor="#E6FFE6" />
    </asp:BoundField>
    </Columns>
    <SelectedRowStyle ForeColor="Blue" />
    </asp:GridView>


    And the vb.net code
    Protected Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating
    Dim hold As String
    Dim RecDate As Date
    Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
    Dim myId As Integer = TryCast(row.Cells(0).Controls(0), TextBox).Text
    Dim myDate As Date = TryCast(row.Cells(3).Controls(0), TextBox).Text
    'RecDate = Date(myDate, "dd-MM-yyyy")
    Dim Ticket As HyperLink = TryCast(GridView1.Rows(e.RowIndex).FindControl("Hyperlink1"), HyperLink)
    Dim tHyperLink As TextBox = CType(row.FindControl("hyperlinkvalue"), TextBox)
    e.NewValues("InvoiceNumber") = tHyperLink.Text
    hold = tHyperLink.Text
    'Dim Invoice As HyperLink = TryCast(row.Cells(2).Controls(0), HyperLink).Text
    Dim Field As Decimal = TryCast(row.Cells(5).Controls(0), TextBox).Text
    Dim Standby As Decimal = TryCast(row.Cells(6).Controls(0), TextBox).Text
    Dim Shop As Decimal = TryCast(row.Cells(7).Controls(0), TextBox).Text
    Dim Travel As Decimal = TryCast(row.Cells(8).Controls(0), TextBox).Text
    Dim SubPay As Integer = TryCast(row.Cells(9).Controls(0), TextBox).Text
    Dim Comments As String = TryCast(row.Cells(10).Controls(0), TextBox).Text
    'Dim Approved As String = TryCast(row.Cells(11).Controls(0), CheckBox).Checked
    'GridView1.EditIndex = -1
    ' Me.BindGrid()
    dbconn = New MySqlConnection("server=xxx,xxx,xxx,xxx;user id=xxxxxx;password=xxxxxx;port=3306;database=xxxxxxx")
    Try
    dbconn.Open()
    Catch ex As Exception
    ' Response.Write("<script>alert('Error');</script>").text
    End Try
    Try
    sql = " Update timesheet SET Date = '" & myDate & "' ,InvoiceNumber= '" & hold & "',FieldHours= '" & Field & "',Shop= '" & Shop & "',Travel= '" & Travel & "',Standby= '" & Standby & "',Comments= '" & Comments & "',SubPay= '" & SubPay & "' where ID = '" & myId & "' "
    ' sql = "Update timesheet Set Date = '" & myDate.Text & "',InvoiceNumber = '" & Hyperlink1.Text & "', FieldHours = '" + Label2.text + "',Standby='" & tbStandby.text & "',Shop='" + tbShop + "', Travel='" & tbTravel.text & "',SubPay='" & tbSubPay.text & "', Comments='" & tbComments.text & "' where ID = " & Convert.ToInt32(id.Text) & ""
    Catch es As Exception
    ' Response.Write("<script>alert('Error');</script>")
    End Try
    Try
    dbcomm = New MySqlCommand(sql, dbconn)
    dbread = dbcomm.ExecuteReader()
    dbread.Close()
    dbconn.Close()
    Catch es As System.Exception

    ' Response.Write(es)
    End Try
    ' con = New SqlConnection(cs)
    ' con.Open()
    ' Dim cmd As SqlCommand = New SqlCommand("Update tbl_Employee set Name='" & name.Text & "',City='" + city.Text & "' where ID=" + Convert.ToInt32(id.Text), con)
    'cmd.ExecuteNonQuery()
    ' con.Close()
    GridView1.EditIndex = -1
    GridView1.DataBind()
    End Sub

  4. #4
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Help with gridview

    double post
    Last edited by NeedSomeAnswers; Nov 8th, 2019 at 04:21 AM.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  5. #5
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Help with gridview

    HTML Code:
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" style="width:100%;overflow:auto" DataKeyNames="id" >
    <Columns>
    <asp:BoundField DataField="id" HeaderText="Id" ControlStyle-Width="1px">
    <ControlStyle Width="1px"></ControlStyle>
    
    <HeaderStyle BorderStyle="None" ForeColor="White" Width="1px" />
    <ItemStyle BorderStyle="None" ForeColor="White" Width="1px" />
    </asp:BoundField>
    <asp:BoundField DataField="Region">
    <ItemStyle ForeColor="#E6FFE6" HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" Wrap="False" />
    </asp:BoundField>
    <asp:TemplateField HeaderText="Employee" ControlStyle-Width="100px">
    <ItemTemplate>
    <asp:HyperLink ID="HyperLink2" runat="server" ControlStyle-Width="75px" Text='<%# Eval("name") %>' NavigateUrl='<%#String.Format("~/Admin/DailyByName.aspx?Id={0}&Name={1}", HttpUtility.UrlEncode(Eval("UserId").ToString()), HttpUtility.UrlEncode(Eval("name").ToString())) %>'></asp:HyperLink>
    </ItemTemplate>
    
    <ControlStyle Width="100px"></ControlStyle>
    
    <ItemStyle BorderColor="Black" ForeColor="Blue" />
    </asp:TemplateField>
    <asp:BoundField DataField="Date" DataFormatString="{0}" HeaderText="Date" ControlStyle-Width="100px" SortExpression="Date">
    <ControlStyle Width="100px"></ControlStyle>
    
    <HeaderStyle Height="50px" Width="140px" HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle Height="50px" Width="140px" HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:BoundField>
    <asp:TemplateField HeaderText="Ticket #">
    <ItemTemplate>
    <asp:HyperLink ID="HyperLink1" runat="server" ControlStyle-Width="75px" Text='<%# Eval("InvoiceNumber") %>' NavigateUrl='<%# Eval("InvoiceNumber", "/Admin/Invoice.aspx?Id={0}") %>' ></asp:HyperLink>
    </ItemTemplate>
    <ItemStyle BorderColor="Black" ForeColor="Blue" />
    <EditItemTemplate>
    <asp:TextBox ID="hyperlinkvalue" Text='<%#Eval("InvoiceNumber")%>' Width="75" runat="server" />
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="FieldHours" HeaderText="Field Hrs" SortExpression="FieldHours" ControlStyle-Width="75px" DataFormatString="{0:#.#;;&quot;&quot;}" >
    <ControlStyle Width="75px"></ControlStyle>
    
    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:BoundField>
    <asp:BoundField DataField="Standby" HeaderText="Standby" SortExpression="Standby" ControlStyle-Width="75px" DataFormatString="{0:#.#;;&quot;&quot;}" >
    <ControlStyle Width="75px"></ControlStyle>
    
    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:BoundField>
    <asp:BoundField DataField="Shop" HeaderText="Shop" SortExpression="Shop" ControlStyle-Width="75px" DataFormatString="{0:#.#;;&quot;&quot;}" >
    <ControlStyle Width="75px"></ControlStyle>
    
    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:BoundField>
    <asp:BoundField DataField="Travel" DataFormatString="{0:#.#;;&quot;&quot;}" ControlStyle-Width="75px" HeaderText="Travel" >
    <ControlStyle Width="75px"></ControlStyle>
    
    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:BoundField>
    <asp:BoundField DataField="SubPay" HeaderText="SubPay" SortExpression="SubPay" ControlStyle-Width="75px" DataFormatString="{0:#.#;;&quot;&quot;}" >
    <ControlStyle Width="75px"></ControlStyle>
    
    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:BoundField>
    <asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" ControlStyle-Width="375px" >
    <ControlStyle Width="375px"></ControlStyle>
    
    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />
    </asp:BoundField>
    <asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID" Visible="False" >
    </asp:BoundField>
    <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
    <asp:TemplateField HeaderText="Approved">
    <ItemTemplate>
    <asp:CheckBox ID="Approved" runat="server" Checked='<%# Bind("Approved") %>' Enabled="true"/>
    </ItemTemplate>
    <HeaderStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Warning">
    <ItemTemplate>
    <asp:CheckBox ID="Warning" runat="server" Checked='<%# Bind("Warning") %>' Enabled="true"/>
    </ItemTemplate>
    <HeaderStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    </asp:TemplateField>
    <asp:BoundField DataField="Salary">
    <ItemStyle HorizontalAlign="Left" Width="1px" ForeColor="#E6FFE6" />
    </asp:BoundField>
    </Columns>
    <SelectedRowStyle ForeColor="Blue" />
    </asp:GridView>


    Code:
    Protected Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating
    Dim hold As String
    Dim RecDate As Date
    Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
    Dim myId As Integer = TryCast(row.Cells(0).Controls(0), TextBox).Text
    Dim myDate As Date = TryCast(row.Cells(3).Controls(0), TextBox).Text
    'RecDate = Date(myDate, "dd-MM-yyyy")
    Dim Ticket As HyperLink = TryCast(GridView1.Rows(e.RowIndex).FindControl("Hyperlink1"), HyperLink)
    Dim tHyperLink As TextBox = CType(row.FindControl("hyperlinkvalue"), TextBox)
    e.NewValues("InvoiceNumber") = tHyperLink.Text
    hold = tHyperLink.Text
    'Dim Invoice As HyperLink = TryCast(row.Cells(2).Controls(0), HyperLink).Text
    Dim Field As Decimal = TryCast(row.Cells(5).Controls(0), TextBox).Text
    Dim Standby As Decimal = TryCast(row.Cells(6).Controls(0), TextBox).Text
    Dim Shop As Decimal = TryCast(row.Cells(7).Controls(0), TextBox).Text
    Dim Travel As Decimal = TryCast(row.Cells(8).Controls(0), TextBox).Text
    Dim SubPay As Integer = TryCast(row.Cells(9).Controls(0), TextBox).Text
    Dim Comments As String = TryCast(row.Cells(10).Controls(0), TextBox).Text
    'Dim Approved As String = TryCast(row.Cells(11).Controls(0), CheckBox).Checked
    'GridView1.EditIndex = -1
    ' Me.BindGrid()
    dbconn = New MySqlConnection("server=xxx,xxx,xxx,xxx;user id=xxxxxx;password=xxxxxx;port=3306;database=xxxxxxx")
    Try
    dbconn.Open()
    Catch ex As Exception
    ' Response.Write("<script>alert('Error');</script>").text
    End Try
    Try
    sql = " Update timesheet SET Date = '" & myDate & "' ,InvoiceNumber= '" & hold & "',FieldHours= '" & Field & "',Shop= '" & Shop & "',Travel= '" & Travel & "',Standby= '" & Standby & "',Comments= '" & Comments & "',SubPay= '" & SubPay & "' where ID = '" & myId & "' "
    ' sql = "Update timesheet Set Date = '" & myDate.Text & "',InvoiceNumber = '" & Hyperlink1.Text & "', FieldHours = '" + Label2.text + "',Standby='" & tbStandby.text & "',Shop='" + tbShop + "', Travel='" & tbTravel.text & "',SubPay='" & tbSubPay.text & "', Comments='" & tbComments.text & "' where ID = " & Convert.ToInt32(id.Text) & ""
    Catch es As Exception
    ' Response.Write("<script>alert('Error');</script>")
    End Try
    Try
    dbcomm = New MySqlCommand(sql, dbconn)
    dbread = dbcomm.ExecuteReader()
    dbread.Close()
    dbconn.Close()
    Catch es As System.Exception
    
    ' Response.Write(es)
    End Try
    ' con = New SqlConnection(cs)
    ' con.Open()
    ' Dim cmd As SqlCommand = New SqlCommand("Update tbl_Employee set Name='" & name.Text & "',City='" + city.Text & "' where ID=" + Convert.ToInt32(id.Text), con)
    'cmd.ExecuteNonQuery()
    ' con.Close()
    GridView1.EditIndex = -1
    GridView1.DataBind()
    End Sub

    When you post code if you use the code tags which is the hash # button in the editor it will format your code, i have done it for you this time but if you could do that in future it would help everyone out
    Last edited by NeedSomeAnswers; Nov 8th, 2019 at 04:19 AM.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  6. #6
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Help with gridview

    have you tried just using bold tags around the text you want bolded e.g. - this is the text i want <b>bolded</b> ?
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2019
    Posts
    8

    Resolved Re: Help with gridview

    Thanks, that will work for 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