Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
955
why the column's order is changed after run button1_Click?
posted

all the codes as below:

    public partial class frmFileDialoge : Form
    {
        UltraGrid dgv = new UltraGrid();
        DataTable dtClone = new DataTable();
        DataTable m_dt=new DataTable();
        string strSql= "Select FormCode、UserCode、FormUS、FormCN、Access、[Add]、[Edit]、[Delete]、[Save]、Upload、Export、[Print]、[Audit]、[Approve]、DBNo from vw_getButtonauthority ";
        string strSql1 = "Select FormCode、UserCode、Access、[Add]、[Edit]、[Delete]、[Save]、Upload、Export、[Print]、[Audit]、[Approve]、DBNo from PB_userright ";
        string strCon = "Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=sql";
        public frmFileDialoge()
        {
            InitializeComponent();
        }

        private void frmFileDialoge_Load(object sender、 EventArgs e)
        {
            SqlDataAdapter da = new SqlDataAdapter(strSql、 strCon);
            DataTable dt = new DataTable();
            da.Fill(dt);
            this.txDataGridViewEdit1.DataSource = dt;
        }

        private void button1_Click(object sender、 EventArgs e)
        {
            DataUpdateGrid();
        }

        private void DataUpdateGrid()
        {
            DataTable m_dt = this.GetGridDataTable();
            ExecuteSqlTran(m_dt);
            dgv.DataSource = dtClone;
        }

        private DataTable GetGridDataTable()
        {
            DataTable dt = new DataTable();
            dtClone = ((DataTable)this.txDataGridViewEdit1.DataSource).Clone();
            dt = (DataTable)this.txDataGridViewEdit1.DataSource;
            dgv = this.txDataGridViewEdit1;
            dt.Columns.Remove("FormUS");
            dt.Columns.Remove("FormCN");
            return dt;
        }

        private  void ExecuteSqlTran(DataTable m_DataTable)
        {
            using (SqlConnection m_Connection = new SqlConnection(strCon))
            {
                m_Connection.Open();
                SqlCommand m_Cmd = new SqlCommand();
                m_Cmd.Connection = m_Connection;
                SqlTransaction m_SqlTran = m_Connection.BeginTransaction();
                m_Cmd.Transaction = m_SqlTran;
                try
                {
                   m_Cmd.CommandText = strSql1;
                   m_Cmd.ExecuteNonQuery();
                   SqlDataAdapter m_SqlDataAdapter = new SqlDataAdapter(strSql1、 strCon );
                   SqlCommandBuilder SqlBuilder = new SqlCommandBuilder(m_SqlDataAdapter);
                   m_SqlDataAdapter.Update(m_DataTable);
                   m_SqlTran.Commit();
                }
                catch (Exception ex)
                {
                    m_SqlTran.Rollback();
                    m_Connection.Dispose();
                }
            }
        }

   }

this ultragid has  FormCode、UserCode、FormUS、FormCN、Access、[Add]、[Edit]、[Delete]、[Save]、Upload、Export、
[Print]、[Audit]、[Approve] and DBNo 15 columns,
I built a data schema, the column order is FormCode、UserCode、FormUS、FormCN、Access、[Add]、[Edit]、[Delete]、[Save]、Upload、Export、
[Print]、[Audit]、[Approve] and DBNo, and FormCode,UserCode and DBNo is hidden,when I save the data schema, I choose the second option.
but after execute the button_click the column order is changed,the Access column is moved before FormCN.
Now how to make the column order keep the previou order?

Thanks in advance!

  • 469350
    Suggested Answer
    Offline posted

    Hi,

    It's very hard to read this code, since many of the variables here are not defined and it's not clear from the names what they are.

    My guess is that you are either re-binding the grid or you are doing something to the grid's DataSource which is causing it so send a Reset notification. This will cause the grid to throw away all rows and columns and re-build them based on the structure provided by the data source.

    The best way to handle something like this is not to cause a reset notification. Resetting the data source is a large and destructive operation and it's almost never the best or most efficient way to do things.

    But if the reset is absolutely neccessary, then what you can do is use the grid.DisplayLayout.Save method to save the layout to a stream before you begin the operation and then load the layout back into the grid afterward.