Datagridview Remove Sort

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • inpuarg

    #1

    Datagridview Remove Sort

    Is it possible to disable sort temporarilty ? without changing current row ?
    I want to add a row to a specific location. But if user sorted the datagridview
    row goes to a wrong position. That 's why before calling InsertAt() method i
    want to remove current sorting. Is it possible ?

  • Frank Dzaebel

    #2
    Re: Datagridview Remove Sort

    Hello,
    [color=blue]
    > Is it possible to disable sort temporarilty ? without changing current row ?
    > I want to add a row to a specific location. But if user sorted the
    > datagridview
    > row goes to a wrong position. That 's why before calling InsertAt() method i
    > want to remove current sorting. Is it possible ?[/color]

    Where should the row be exactly placed?
    Have in mind this could only be a really temporary state.
    The User *wants* the Grid to be sorted (in this case)
    Ok, what if you use the following. What do you wish?

    //using AdrRow=DataForm Assist.Adventur eWorksDataSet.A ddressRow;
    DataGridView dgv = documentDataGri dView;
    DataRow dr = ((DataRowView)a ddressBindingSo urce.AddNew()). Row;
    AdrRow adr = (AdrRow)dr;
    adr.AddressLine 1="Hallo1"; adr.AddressLine 2="Hallo2";
    adr.City = "MyCity"; addressBindingS ource.EndEdit() ;


    //=============== =========

    if you really want a that what you said, you
    could bind to a newly created temporary DataTable like:


    CurrencyManager cm = (CurrencyManage r)BindingContex t[
    dgv.DataSource, dgv.DataMember];
    DataTable dt = new DataTable("Temp ");
    foreach (DataGridViewCo lumn dgc in dgv.Columns)
    dt.Columns.Add( dgc.DataPropert yName, dgc.ValueType);
    int oldPosition = cm.Position;
    DataRowView darv = ((DataRowView)a ddressBindingSo urce.AddNew());
    AdrRow adr = (AdrRow)darv.Ro w;
    adr.AddressLine 1 = "Hello1"; adr.AddressLine 2 = "Hello2"; adr.City = "MyCity";
    DataRow yourNewRow = dt.NewRow();
    yourNewRow.Item Array = adr.ItemArray;
    for (int r=0; r<cm.List.Count ; r++)
    {
    DataRowView drv = (DataRowView)cm .List[r];
    DataRow dr = dt.NewRow();
    dr.ItemArray = drv.Row.ItemArr ay;
    if (r == oldPosition) dt.Rows.Add(you rNewRow);
    if (r != cm.Position) dt.Rows.Add(dr) ;
    }
    dgv.DataSource = dt; cm.Position = oldPosition;
    dgv.CurrentCell = dgv.Rows[cm.Position].Cells[0];


    ciao Frank
    --
    Dipl.Inf. Frank Dzaebel [MCP/MVP C#]


    Comment

    Working...