排序后我的网格视图没有刷新

时间:2019-07-14 07:56:06

标签: c# asp.net

我在项目中有一个网格视图,并编写了将网格视图中的日期转换为波斯日历日期的代码

第一次以网格视图显示所有数据时,我的日期都正确地以波斯格式显示,但是如果我按排序按钮,则它再次以英语格式显示日期

排序后如何保留波斯格式的日期?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Globalization;
public partial class dr_report : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
          GridView2.Visible = false;
          string userinput = "";
          int c = GridView1.Rows.Count;
          for (int i = 0; i < GridView1.Rows.Count; i++)
          {
              userinput = GridView1.Rows[i].Cells[4].Text;
              System.DateTime date = Convert.ToDateTime(userinput);
              System.Globalization.PersianCalendar p = new System.Globalization.PersianCalendar();

              int year = p.GetYear(date);
              int month = p.GetMonth(date);
              int day = p.GetDayOfMonth(date);
              System.DateTime currentDate = new System.DateTime(year, month, 1);
              currentDate = currentDate.AddDays(day - 1);

              GridView1.Rows[i].Cells[4].Text = currentDate.ToString("dd/MM/yyyy");


           }
         }
      }




     protected void Button1_Click(object sender, EventArgs e)
     {
       GridView1.Visible = false;
       GridView2.Visible = true;
       SqlConnection con = new SqlConnection();
       con.ConnectionString = "Data Source=.;Initial Catalog=daymond;Integrated Security=True";
       SqlCommand com = new SqlCommand();
       SqlCommand com1 = new SqlCommand();
       SqlDataAdapter dat = new SqlDataAdapter();
       DataTable dt = new DataTable();
       com.Connection = con;
       con.Open();

       string query = "select [usercustomer], [uservisitor], [idcustomer], [takhasos], [date] from [customer] where idcustomer = '" + TextBox1.Text + "'";
       com.CommandText = query;
       com.Connection = con;
       com1.CommandText = "Delete from [customer] where [idcustomer]=1255";
       dat.DeleteCommand =com1;
       dat.SelectCommand = com;
       dat.Fill(dt);
       GridView2.DataSource = dt;
       GridView2.DataBind();
     }



   protected void LinkButton1_Click(object sender, EventArgs e)
   {
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=daymond;Initial Catalog=daymond;Integrated Security=True";
    SqlCommand com = new SqlCommand();
    com.Connection = con;
    con.Open();
    SqlDataAdapter Adapter = new SqlDataAdapter();
    System.Data.DataTable dt = new System.Data.DataTable();

    Session.Remove("users");
    Response.Redirect("index.aspx");
   }




  protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
  {

  }
 }

3 个答案:

答案 0 :(得分:0)

我认为您必须在绑定到gridview之前将sortExpression存储到一个viewstate中并对数据源进行排序

DataView dv = ds.Tables["Test"].DefaultView;

    // Set the sort column and sort order.
    dv.Sort = ViewState["SortExpression"].ToString(); 

    // Bind the GridView control.
    GridView1.DataSource = dv;
    GridView1.DataBind();

Check this

答案 1 :(得分:0)

我会将您的代码移至RowCreated事件。

void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
  {


    if(e.Row.RowType == DataControlRowType.DataRow)
    {
    string userInput = e.Row.Cells[4].Text;
    System.DateTime date = Convert.ToDateTime(userInput);
    System.Globalization.PersianCalendar p = newSystem.Globalization.PersianCalendar(); 

    int year = p.GetYear(date);
    int month = p.GetMonth(date);
    int day = p.GetDayOfMonth(date);
    System.DateTime currentDate = new System.DateTime(year, month, 1);
    currentDate = currentDate.AddDays(day - 1);
    e.Row.Cells[4].Text = currentDate.ToString();
    }
  }

答案 2 :(得分:0)

它不是页面的Page_Load事件使用页面的OnSaveStateComplete事件,而是在页面生命周期结束时触发。更多信息Page_Cycle in asp.net

因此,当Grid视图与源绑定并对该列进行排序时,您可以应用逻辑来更改日期格式

 protected override void OnSaveStateComplete(EventArgs e)
        {
            string userinput = "";
            int c = GridView1.Rows.Count;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                userinput = GridView1.Rows[i].Cells[4].Text;


                System.DateTime date = Convert.ToDateTime(userinput);
                System.Globalization.PersianCalendar p = new System.Globalization.PersianCalendar();

                int year = p.GetYear(date);
                int month = p.GetMonth(date);
                int day = p.GetDayOfMonth(date);
                System.DateTime currentDate = new System.DateTime(year, month, 1);
                currentDate = currentDate.AddDays(day - 1);

                GridView1.Rows[i].Cells[4].Text = currentDate.ToString("dd/MM/yyyy");

            }
        }