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 1 users browsing this thread. (0 members and 1 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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.09377 seconds
  • Memory Usage 2,889KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (3)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates