• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1426
  • Last Modified:

deselect row in gridview on refresh

When I select gridview row on button select it change color and font, but when I click refresh I want to have gridview without any row selected.
Also if I select row and insert it in sql table on button click (button is not in gridview) and after that click on refresh button in browser selected row inserts again in sql table without clicking on it.
How can I solve this?
Thanks
0
kahvedzic
Asked:
kahvedzic
  • 9
  • 8
  • 3
  • +1
2 Solutions
 
Reza RadCommented:
use this line:
yourgridviewname.SelectedRows.Clear();
0
 
waltersnowslinarnoldCommented:
Guess you must have located your GridView bind inside if(!IsPostBack){}

Can you post a sample of your code??
0
 
kahvedzicAuthor Commented:
I add this line of code at the end on button event for inserting in sql table, now situation is that when I click on button it insert data in sql table and deselect row but when I click refresh after that it again insert same data in table.
Where do I have to put this code so that no row is selected after just clicking refresh button. Example, I select one row ,then click refresh and no row is selected.
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
waltersnowslinarnoldCommented:
Dont put your insert query in pageload, better put that inside an event, may be in a button. else the same problem persists..
0
 
Reza RadCommented:
after calling databind() method of grid put the code which i suggested to you
0
 
kahvedzicAuthor Commented:
here is the code behing and I put
GridView1.SelectedRow.Cells.Clear()
after GridView1.DataBind() but still after page refresh have row selected

Imports Domino
Imports System.Data.SqlClient

Partial Class Default6
    Inherits System.Web.UI.Page
  Private Function CreateDataTable() As Data.DataTable
        Try
            Dim myDataTable As Data.DataTable = New Data.DataTable()
            Dim myDataColumn As Data.DataColumn

            myDataColumn = New Data.DataColumn()
            myDataColumn.DataType = Type.GetType("System.Int32")
            myDataColumn.ColumnName = "ID"
            myDataTable.Columns.Add(myDataColumn)

            myDataColumn = New Data.DataColumn()
            myDataColumn.DataType = Type.GetType("System.String")
            myDataColumn.ColumnName = "Status"
            myDataTable.Columns.Add(myDataColumn)

            myDataColumn = New Data.DataColumn()
            myDataColumn.DataType = Type.GetType("System.String")
            myDataColumn.ColumnName = "From"
            myDataTable.Columns.Add(myDataColumn)

            myDataColumn = New Data.DataColumn()
            myDataColumn.DataType = Type.GetType("System.String")
            myDataColumn.ColumnName = "To"
            myDataTable.Columns.Add(myDataColumn)

            myDataColumn = New Data.DataColumn()
            myDataColumn.DataType = Type.GetType("System.String")
            myDataColumn.ColumnName = "Timestamp"
            myDataTable.Columns.Add(myDataColumn)

            myDataColumn = New Data.DataColumn()
            myDataColumn.DataType = Type.GetType("System.String")
            myDataColumn.ColumnName = "Subject"
            myDataTable.Columns.Add(myDataColumn)

            myDataColumn = New Data.DataColumn()
            myDataColumn.DataType = Type.GetType("System.String")
            myDataColumn.ColumnName = "Attachment List"
            myDataTable.Columns.Add(myDataColumn)

            myDataColumn = New Data.DataColumn()
            myDataColumn.DataType = Type.GetType("System.String")
            myDataColumn.ColumnName = "Body"
            myDataTable.Columns.Add(myDataColumn)

            Return myDataTable
        Catch ex As Exception

        End Try

    End Function

    Private Function AddDataToTable(ByVal bdy As String, ByVal fom As String, ByVal sbj As String, ByVal time As String, ByVal attachmentNames As String, ByVal myTable As Data.DataTable)
        Try
            Dim row As Data.DataRow = myTable.NewRow()

            row("From") = fom
            row("Subject") = sbj
            row("Timestamp") = Convert.ToDateTime(time).ToString("yyyy-MM-dd HH:mm") 'time
            row("Attachment List") = attachmentNames
            row("Body") = bdy

            myTable.Rows.Add(row)
            myTable.AcceptChanges()
        Catch ex As Exception

        End Try
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim appNotes As NotesSession
        appNotes = New NotesSession
        Dim dbNotes As NotesDatabase
appNotes.Initialize("abc")
        dbNotes = appNotes.GetDatabase("abc", "abc")
        Dim vwInbox As NotesView
        vwInbox = dbNotes.GetView("$Inbox")
        Dim docMessage As NotesDocument
        Dim fom As Object
        Dim sbj As Object
        Dim bdy As Object
        Dim time As Object
        Dim attachmentNames As Object
        Dim prezime As String
        Dim vrijeme As String
        On Error Resume Next
Dim myDt As Data.DataTable
        myDt = New Data.DataTable()
        myDt = CreateDataTable()

        Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim mySqlConnection As New SqlConnection(connectionString)
        Dim cmd As New SqlCommand
        Dim dreader As SqlDataReader
        If mySqlConnection.State = Data.ConnectionState.Open Then mySqlConnection.Close()
        mySqlConnection.Open()

        With vwInbox
            docMessage = .GetLastDocument
            Do
                fom = appNotes.Evaluate("@Name([CN]; From)", docMessage)
                'fom = appNotes.Evaluate("@Right(@Left(From; ""/""); ""CN="")", docMessage)

                sbj = appNotes.Evaluate(("Subject"), docMessage)

                bdy = appNotes.Evaluate("@Text(Body)", docMessage)
                'List3.Items.Add(docMessage.GetItemValue("Body"))
                'List3.Items.Add(Join(docMessage.GetItemValue("Body")))

                time = appNotes.Evaluate("@Text(DeliveredDate)", docMessage)
                Convert.ToDateTime(time).ToString("yyyy-MM-dd HH:mm")

                attachmentNames = appNotes.Evaluate("@Implode(@AttachmentNames; "", "")", docMessage)
                If attachmentNames(0) = "" Then attachmentNames(0) = " "
                docMessage = .GetPrevDocument(docMessage)
                AddDataToTable(CStr(bdy(0)), CStr(fom(0)), CStr(sbj(0)), CStr(time(0)), CStr(attachmentNames(0)), myDt)

            Loop Until docMessage Is Nothing
        End With
        On Error Resume Next

        docMessage = Nothing
        vwInbox = Nothing
        dbNotes = Nothing
        appNotes = Nothing

        For i As Integer = 0 To myDt.Rows.Count - 1
            cmd.Parameters.Clear()
            prezime = myDt.Rows(i).Item("From")
            vrijeme = Convert.ToDateTime(myDt.Rows(i).Item("Timestamp")).ToString("yyyy-MM-dd HH:mm")
            cmd.Connection = mySqlConnection
            cmd.CommandText = "SELECT ID,Status,Uradio from Zahtjev_za_intervenciju with (nolock) where Prezime_ime=@prezime and [Timestamp]=@vrijeme;"

            cmd.Parameters.AddWithValue("@prezime", prezime.ToString)
            cmd.Parameters.AddWithValue("@vrijeme", Convert.ToDateTime(vrijeme).ToString("yyyy-MM-dd HH:mm"))
            dreader = cmd.ExecuteReader
            If dreader.HasRows Then
                dreader.Read()
                myDt.Rows(i).Item("ID") = dreader.Item("ID")
                myDt.Rows(i).Item("To") = dreader.Item("Uradio")
                myDt.Rows(i).Item("Status") = dreader.Item("Status")

            End If
            dreader.Close()
        Next

        mySqlConnection.Close()

        'If IsPostBack = False Then
        Session("myDataTable") = myDt
        Me.GridView1.DataSource = myDt
        Me.GridView1.Attributes.Add("style", "table-layout:dynamic")
        Me.GridView1.AutoGenerateColumns = True
        Me.GridView1.DataBind()
        Me.GridView1.SelectedRow.Cells.Clear()
        'End If

        hidUser.Value = Session("user")
        hidPass.Value = Session("pass")
        hidUser1.Value = Session("user1")
        hidPass1.Value = Session("pass1")
        hidUser2.Value = Session("user2")
        hidPass2.Value = Session("pass2")
        hidUser3.Value = Session("user3")
        hidPass3.Value = Session("pass3")

        'Remember to change these to the same as what you will make on the loginPage.aspx 
        If hidUser.Value <> "Daniel FRIMEL" And hidPass.Value <> "hd3$" Then
            Response.Redirect("Default.aspx")
        End If

        If hidUser1.Value <> "Elvis ZECIC" And hidPass1.Value <> "hd3$" Then
            Response.Redirect("Default.aspx")
        End If

        If hidUser2.Value <> "Fuad GADZO" And hidPass2.Value <> "hd3$" Then
            Response.Redirect("Default.aspx")
        End If

        If hidUser3.Value <> "Sanda NOVOTNI-DUJMOVIC" And hidPass3.Value <> "hd3$" Then
            Response.Redirect("Default.aspx")
        End If

    End Sub

    Public Sub GridView1_OnRowCreated(ByVal sender As Object, ByVal e As Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated

        e.Row.Cells(7).Visible = False
        e.Row.Cells(8).Visible = False

    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

        txtBody.Text = GridView1.SelectedRow.Cells(8).Text
        txtAttach.Text = GridView1.SelectedRow.Cells(7).Text

        Dim selektovan As String
        selektovan = GridView1.SelectedRow.Cells(1).Text
        If selektovan = "&nbsp;" Then
            Upisi.Enabled = True
        Else
            Upisi.Enabled = False
        End If

    End Sub

    Protected Sub Upisi_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Upisi.Click

        Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim mySqlConnection As New SqlConnection(connectionString)
        Dim vrijeme2 As String
        vrijeme2 = Convert.ToDateTime(GridView1.SelectedRow.Cells(5).Text).ToString("yyyy-MM-dd HH:mm")

        If mySqlConnection.State = Data.ConnectionState.Open Then mySqlConnection.Close()
        mySqlConnection.Open()

        Dim strSqlInsert As String = "INSERT INTO Zahtjev_za_intervenciju (Prezime_ime, Opis_Lotus, Timestamp, Subject_Lotus, Atach_list, Lotus_Intranet) VALUES (@Prezime_ime, @Opis_Lotus, @Timestamp, @Subject_Lotus, @Atach_List, @Lotus_Intranet);"
        Dim mySqlCommand As SqlCommand = New SqlCommand(strSqlInsert, mySqlConnection)

        mySqlCommand.Parameters.AddWithValue("@Prezime_ime", GridView1.SelectedRow.Cells(3).Text)
        mySqlCommand.Parameters.AddWithValue("@Subject_Lotus", GridView1.SelectedRow.Cells(6).Text)
        mySqlCommand.Parameters.AddWithValue("@Opis_Lotus", txtBody.Text)
        mySqlCommand.Parameters.AddWithValue("@Timestamp", Convert.ToDateTime(vrijeme2).ToString("yyyy-MM-dd HH:mm"))
        mySqlCommand.Parameters.AddWithValue("@Atach_List", txtAttach.Text)
        mySqlCommand.Parameters.AddWithValue("@Lotus_Intranet", "L")

        mySqlCommand.ExecuteNonQuery()
        mySqlConnection.Close()

    End Sub
End Class

Open in new window

0
 
waltersnowslinarnoldCommented:
Why do you call AddDataToTable function in pageload.,

AddDataToTable(CStr(bdy(0)), CStr(fom(0)), CStr(sbj(0)), CStr(time(0)), CStr(attachmentNames(0)), myDt).

The above creates a new DataTable every time the page loads. just the datable with the select query binded with the grid is good enough to show the grid
0
 
kahvedzicAuthor Commented:
What I need is complete content of mail inbox every time I refresh page. When Gridview loads I want to select row and if I click refresh it will display any new mail from inbox and deselect row from gridview
0
 
waltersnowslinarnoldCommented:
May I know what problem you face as of now? I couldn't spot your difficulty??

I understood as, every time you refresh a new record is inserted into database.
0
 
Reza RadCommented:
don't use:

GridView1.SelectedRow.Cells.Clear();

just use
GridView1.SelectedRow.Clear();
0
 
kahvedzicAuthor Commented:
Yes exactly that is my problem, when I load gridview i want to gridview persist same without new rows (after row select and after on button click for insert in sql table). I want to refresh data in gridview only when I click on refresh button in web browser
0
 
waltersnowslinarnoldCommented:
Then put your GridView code inside

if(!IsPostBack)
{
  GridView.DataSource = DataTable;
  GridView.DataBind();
}

And,
Insert code inside Button_Click event..
0
 
kahvedzicAuthor Commented:
I did this way and no gridview loads

If Page.IsPostBack Then
Session("myDataTable") = myDt
Me.GridView1.DataSource = myDt
Me.GridView1.Attributes.Add("style", "table-layout:dynamic")
Me.GridView1.AutoGenerateColumns = True
Me.GridView1.DataBind()
End If
Insert code is as you can see from codebehind in Button_click event
0
 
waltersnowslinarnoldCommented:
has the DataTable myDt, loaded with data. guess here your grid is empty.. fill the DataTable
0
 
kahvedzicAuthor Commented:
i just step through code and datatable is filled, but Me.GridView1.DataSource = myDt  says false and no gridview loads. If i remove If Page.IsPostBack  everything works

0
 
waltersnowslinarnoldCommented:
Yeah it will, but it will load every pageload, I believe that's what is your need..rite?
0
 
kahvedzicAuthor Commented:
lets just simply say, when I first load GridView on PageLoad, after that only refresh of gridview must be only at click on refresh button in web browser. No new rows on row select, no new rows on insert button click only on refresh button to manualy refresh page and gridview, is it possible this way? Thanks
0
 
waltersnowslinarnoldCommented:
For the above need, you should put the gridview inside if(!Page.IsPostBack). within this, you should fill the datatable as well..
0
 
kumar754Commented:
to remove the Grid Selection after inserting data, add this:
GridView1.SelectedIndex = -1;

also you can check if the browser is refresh, there are some tricks you can add to your code, follow this url:
http://prashantaware.blogspot.com/2009/10/deducting-browser-refresh.html
0
 
kahvedzicAuthor Commented:
What I did put complete page load in
If Page.IsPostBack = False Then, now I dont have any new rows in gridview on select row or insert button click, which is OK, but when I click refresh on web browser it does not refresh my gridview to check for any new mail.
If I use If Page.IsPostBack = True Then, no GridView loads.
What I need to have is to load GridView with mails that are in inbox select one row click on insert, insert row to sql table and click refresh in browser to collect new mails that come into inbox after girdview was loaded on page load.
0
 
kahvedzicAuthor Commented:

I did this

If Not IsPostBack Then
Session("myDataTable") = myDt
Me.GridView1.DataSource = myDt
Me.GridView1.Attributes.Add("style", "table-layout:dynamic")
Me.GridView1.AutoGenerateColumns = True
Me.GridView1.DataBind()
End If

and on submit button at the end add this

Response.Redirect(Request.RawUrl)
after that I add refresh button on my form with same code
Response.Redirect(Request.RawUrl)
now I have just what I want, I refresh page on my refresh button and prevent duplicate record insertion.

Thanks
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 9
  • 8
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now