www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] gridview sorting and editing issue

  1. #1
    Join Date
    Aug 2008
    Posts
    22

    resolved [RESOLVED] gridview sorting and editing issue

    hi,
    i have a gridview with sorting and editing and paging ability.
    i am binding data from xml file.
    i can edit it's rows if i do not sort it.
    but if i sorted it and click edit link, selected row is changing(to row in gridview's first binding situation) and my sorting is off.
    where am i wrong?

    asp.net 2.0, .net 2005.

    if you want, i'll send the code soon.

    thanks.

    my code:

    Code:
    //My databinding method
    protected void doldur()
            {
                DataSet ds = new DataSet();
                ds.ReadXml(yolXml);
                gvProgramlar.DataSource = ds;
                gvProgramlar.DataBind();
            }//protected void doldur()       
    
    //My sorting method
    protected DataView dtSirala(DataTable dataTable, bool iseSayfaNumarasiDegismis)
            {
                if (dataTable != null)
                {
                    DataView dataView = new DataView(dataTable);
                    if (gvSiralamaIfadesi != string.Empty)
                    {
                        if (iseSayfaNumarasiDegismis)
                        {
                            dataView.Sort = string.Format("{0} {1}", gvSiralamaIfadesi, gvSiralamaYonu);
                        }
                        else
                        {
                            dataView.Sort = string.Format("{0} {1}", gvSiralamaIfadesi, siralamaYonunuGoster());
                        }
                    }
                    return dataView;
                }
                else
                {
                    return new DataView();
                }
            }//protected DataView dtSirala(DataTable dataTable, bool iseSayfaNumarasiDegismis)
    
    
    //my sorting method
            protected void gvProgramlarSirala(object sender, GridViewSortEventArgs e)
            {
                siralamaIfadesi = e.SortExpression;
                doldur();
                gvSiralamaIfadesi = siralamaIfadesi;
                int pageIndex = gvProgramlar.PageIndex;
                gvProgramlar.DataSource = dtSirala(((DataSet)(
    gvProgramlar.DataSource)).Tables[0] as DataTable, false);
                gvProgramlar.DataBind();
                gvProgramlar.PageIndex = pageIndex;
                gvProgramlar.EditIndex = gvProgramlar.SelectedIndex;
            }//protected void gvProgramlarSirala(object sender, GridViewSortEventArgs e)
    
    //my editing method
    protected void nodDuzenle(object s, GridViewEditEventArgs e)
            {
                gvProgramlar.EditIndex = e.NewEditIndex;
                doldur();
            }//protected void nodDuzenle(object s, GridViewEditEventArgs e)
    Last edited by mugalata; 01-15-2009 at 10:19 AM.

  2. #2
    Join Date
    Nov 2008
    Posts
    25
    Regarding the edits: My initial guess is that you're not saving the edits in the datasource before calling DataBind(). The DataBind() function re-reads the data from the datasource and replaces the values in the DataGrid control with that data.

    Regarding the page index/sorting: Here's a sample of the code that I use, hopefully it will be helpful to you.

    Thanks.
    -Doug

    Code:
    	
    Private Sub PopulateData(Optional ByVal PageIndex As Integer = 0)
            Dim query As String = "My Query"
            Dim cmd As New SqlCommand(query, _conn)
            daDataAdapter.SelectCommand = cmd
            Try
                daDataAdapter.Fill(dsDataSet)
                DataGrid1.DataSource = dsDataSet.Tables(0)
                DataGrid1.CurrentPageIndex = PageIndex
    
                If Not ViewState("tablesort") Is Nothing Then
                    dsDataSet.Tables(0).DefaultView.Sort = ViewState("tablesort") & " " & ViewState("sortdirection")
                End If
    
                If Not ViewState("SearchParameters") Is Nothing And ViewState("SearchParameters") <> "" Then
                    dsDataSet.Tables(0).DefaultView.RowFilter = ViewState("SearchParameters")
                Else
                DataGrid1.DataBind()
            Catch ex As Exception
    			//LogErrors
            End Try
        End Sub
    Note: This code is pasted from a .NET 1.1/VB project, but it should be 99% the same for a .NET 2.0 project and easily converted to C#.

  3. #3
    Join Date
    Aug 2008
    Posts
    22
    i could not solve it.
    my whole code is this:

    Code:
    public partial class Programlar : System.Web.UI.Page
        {
    
            string yolXml;
            string siralamaIfadesi = "";
            protected void Page_Load(object sender, EventArgs e)
            {
                yolXml = Server.MapPath("./") + "Programlar.xml";
                if (!IsPostBack)
                {
                    doldur();
                }
                if (!GenelMetotlar.iseYonetici(Session["yonetici"]))//Değişecek
                {
                    CommandField objCmd = new CommandField();
                    objCmd = (CommandField)gvProgramlar.Columns[4];
                    objCmd.ShowEditButton = true;
                    objCmd = (CommandField)gvProgramlar.Columns[5];
                    objCmd.ShowDeleteButton = true;
                    divEkle.Visible = true;
                }
            }
    
            protected void doldur()
            {
                if (Session["gvProgramlar"] == null)
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(yolXml);
                    Session["gvProgramlar"] = ds;
                    gvProgramlar.DataSource = ds;
                    gvProgramlar.DataBind();
                }
                else
                {
                    gvProgramlar.DataSource = (DataSet)Session["gvProgramlar"];
                    gvProgramlar.DataBind();
                }
    
            }//protected void doldur()
    
            protected void nodDuzenle(object s, GridViewEditEventArgs e)//OnRowEditing
            {
                gvProgramlar.EditIndex = e.NewEditIndex;
                doldur();
            }//protected void nodDuzenle(object s, GridViewEditEventArgs e)
    
            protected void nodSil(object s, GridViewDeleteEventArgs e)//OnRowDeleting
            {
                doldur();
                DataSet ds = gvProgramlar.DataSource as DataSet;
                ds.Tables[0].Rows[gvProgramlar.Rows[e.RowIndex].DataItemIndex].Delete();
                xmlEYaz(ds);
                doldur();
            }//protected void nodSil(object s, GridViewDeleteEventArgs e)
    
            protected void nodVazgec(object s, GridViewCancelEditEventArgs e)//OnRowCancelingEdit
            {
                gvProgramlar.EditIndex = -1;
                doldur();
            }//protected void nodVazgec(object s, GridViewCancelEditEventArgs e)
    
            protected void nodGuncelle(object s, GridViewUpdateEventArgs e)//OnRowUpdating
            {
                int i = e.RowIndex;
                string strAdi = (gvProgramlar.Rows[i].FindControl("txtADI") as TextBox).Text;
                string strLinki = (gvProgramlar.Rows[i].FindControl("txtLINKI") as TextBox).Text;
                string strAciklamasi = (gvProgramlar.Rows[i].FindControl("txtACIKLAMASI") as TextBox).Text;
                string strIdsi = (gvProgramlar.Rows[i].FindControl("txtIDSI") as TextBox).Text;
                gvProgramlar.EditIndex = -1;
                doldur();
                DataSet ds = (DataSet)gvProgramlar.DataSource;
                ds.Tables[0].Rows[i]["ADI"] = strAdi;
                ds.Tables[0].Rows[i]["LINKI"] = strLinki;
                ds.Tables[0].Rows[i]["ACIKLAMASI"] = strAciklamasi;
                ds.Tables[0].Rows[i]["IDSI"] = strIdsi;
                xmlEYaz(ds);
                doldur();
            }//protected void nodGuncelle(object s, GridViewUpdateEventArgs e)
    
            protected void ekle(object sender, EventArgs e)
            {
                doldur();
                DataSet ds = gvProgramlar.DataSource as DataSet;
                DataRow dr = ds.Tables[0].NewRow();
                dr[0] = ADI.Text;
                dr[1] = LINKI.Text;
                dr[2] = ACIKLAMASI.Text;
                dr[3] = IDSI.Text;
                ds.Tables[0].Rows.Add(dr);
                ds.AcceptChanges();
                xmlEYaz(ds);
                doldur();
                ADI.Text = string.Empty;
                LINKI.Text = string.Empty;
                ACIKLAMASI.Text = string.Empty;
                IDSI.Text = string.Empty;
            }//protected void ekle(object sender, EventArgs e)
    
            private string gvSiralamaYonu
            {
                get { return ViewState["SortDirection"] as string ?? "ASC"; }
                set { ViewState["SortDirection"] = value; }
            }//private string gvSiralamaYonu
    
            private string gvSiralamaIfadesi
            {
                get { return ViewState["SortExpression"] as string ?? string.Empty; }
                set { ViewState["SortExpression"] = value; }
            }//private string gvSiralamaIfadesi
    
            private string siralamaYonunuGoster()
            {
                switch (gvSiralamaYonu)
                {
                    case "ASC":
                        gvSiralamaYonu = "DESC";
                        break;
                    case "DESC":
                        gvSiralamaYonu = "ASC";
                        break;
                }
                return gvSiralamaYonu;
            }//private string siralamaYonunuGoster()
    
            protected void sayfaDegisti(object sender, GridViewPageEventArgs e)//OnPageIndexChanging
            {
                doldur();
                gvProgramlar.DataSource = dtSirala(((DataSet)(gvProgramlar.DataSource)).Tables[0] as DataTable, true);
                gvProgramlar.PageIndex = e.NewPageIndex;
                gvProgramlar.DataBind();
            }//protected void sayfaDegisti(object sender, GridViewPageEventArgs e)
    
            protected DataView dtSirala(DataTable dataTable, bool iseSayfaNumarasiDegismis)
            {
                if (dataTable != null)
                {
                    DataView dataView = new DataView(dataTable);
                    if (gvSiralamaIfadesi != string.Empty)
                    {
                        if (iseSayfaNumarasiDegismis)
                        {
                            dataView.Sort = string.Format("{0} {1}", gvSiralamaIfadesi, gvSiralamaYonu);
                        }
                        else
                        {
                            dataView.Sort = string.Format("{0} {1}", gvSiralamaIfadesi, siralamaYonunuGoster());
                        }
                    }
                    return dataView;
                }
                else
                {
                    return new DataView();
                }
            }//protected DataView dtSirala(DataTable dataTable, bool iseSayfaNumarasiDegismis)
    
            protected void gvProgramlarSirala(object sender, GridViewSortEventArgs e)//OnSorting
            {
                doldur();
                siralamaIfadesi = e.SortExpression;
                gvSiralamaIfadesi = siralamaIfadesi;
                int pageIndex = gvProgramlar.PageIndex;
                DataTable dt = ((DataSet)(gvProgramlar.DataSource)).Tables[0];
                gvProgramlar.DataSource = dtSirala(dt, false);
                gvProgramlar.DataBind();
                gvProgramlar.PageIndex = pageIndex;
            }//protected void gvProgramlarSirala(object sender, GridViewSortEventArgs e)
    
            protected override void OnPreRender(EventArgs e)
            {
                base.OnPreRender(e);
                DataView dv = null;
                if ((gvProgramlar.DataSource).GetType().Name.Equals("DataSet"))
                {
                    DataTable dt = ((DataSet)(gvProgramlar.DataSource)).Tables[0];
                    dv = new DataView(dt);
                }
                else if ((gvProgramlar.DataSource).GetType().Name.Equals("DataView"))
                {
                    dv = (DataView)(gvProgramlar.DataSource);
                }
                else if ((gvProgramlar.DataSource).GetType().Name.Equals("DataTable"))
                {
                    dv = ((DataTable)(gvProgramlar.DataSource)).DefaultView;
                }
                if (siralamaIfadesi != "")
                {
                    dv.Sort = siralamaIfadesi + " " + gvSiralamaYonu;
                }
                else
                {
                    //dv.Sort = dv.Table.Columns[0].ColumnName + " " + siralamaYonunuGoster(); //İlk seferde ada göre sıralasın.
                }
                gvProgramlar.DataSource = dv;
                gvProgramlar.DataBind();
                siralamaResminiEkle();
            }//protected override void OnPreRender(EventArgs e)
    
            public void siralamaResminiEkle()
            {
                if (siralamaIfadesi == "") return;
    
                Image sortImage = new Image();
                sortImage.Style["padding-left"] = "10px";
                // sortImage.Style["padding-bottom"] = "5px";
    
                if (gvSiralamaYonu == "ASC")
                {
                    sortImage.ImageUrl = "resimler/asagi.gif";
                    sortImage.ToolTip = "Artan sıra";
                    sortImage.AlternateText = "Artan sıra";
                }
                else
                {
                    sortImage.ImageUrl = "resimler/yukari.gif";
                    sortImage.ToolTip = "Azalan sıra";
                    sortImage.AlternateText = "Azalan sıra";
                }
    
                // Hangi kolon tiklanmis
                for (int i = 0; i < gvProgramlar.Columns.Count; i++)
                {
                    DataControlField dcf = gvProgramlar.Columns[i];
                    if (dcf.SortExpression == siralamaIfadesi)
                    {
                        gvProgramlar.HeaderRow.Cells[i].Controls.Add(sortImage);
                        break;
                    }
                }
            }//public void siralamaResminiEkle()
    
            private void xmlEYaz(DataSet pDs)
            {
                XmlWriter writer = new XmlTextWriter(yolXml, System.Text.Encoding.GetEncoding("ISO-8859-9"));
                writer.WriteStartDocument();
                pDs.WriteXml(writer,XmlWriteMode.WriteSchema);
                writer.WriteEndDocument();
                writer.Close();
            }
        }

  4. #4
    Join Date
    Aug 2008
    Posts
    22
    it's solved.

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
    doldur();
    siralamaIfadesi = e.SortExpression;
    gvSiralamaIfadesi = siralamaIfadesi;
    int pageIndex = gvProgramlar.PageIndex;
    DataTable dt = ((DataSet)(gvProgramlar.DataSource)).Tables[0];
    gvProgramlar.DataSource = dtSirala(dt, false);
    gvProgramlar.DataBind();
    gvProgramlar.PageIndex = pageIndex;
    ViewState["dt"] = ((DataView)gvProgramlar.DataSource).ToTable();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
    gvProgramlar.EditIndex = e.NewEditIndex;
    gvProgramlar.DataSource = (DataTable)ViewState["dt"];
    gvProgramlar.DataBind();
    }

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles