turn off sorting on my gridview C#

Jayesh Acharya
Jayesh Acharya used Ask the Experts™
on
I have dynamically built my grid view in C#, and the defult build seems to always build each column with the ability to sort its data, how can i turn off this function ?

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Todd GerbertIT Consultant
Top Expert 2010

Commented:
The GridView has an "AllowSorting" property, set it to false.
 
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.allowsorting.aspx 
Jayesh AcharyaTechnichal Consultant

Author

Commented:
this is a win form grid, and the Allow sorting proprty is not an option

        public void InitializeGrid(DataGridView lv_StockCheckdataGridView
                                  ,string GridViewName)
        {
           
            lv_StockCheckdataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            lv_StockCheckdataGridView.ContextMenuStrip = this.contextMenuStrip1;
            lv_StockCheckdataGridView.AllowUserToAddRows = false;
            lv_StockCheckdataGridView.AllowUserToDeleteRows = false;
            lv_StockCheckdataGridView.AllowUserToOrderColumns = true;
            lv_StockCheckdataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            lv_StockCheckdataGridView.Size = new System.Drawing.Size(1074, 604);
            lv_StockCheckdataGridView.TabIndex = 5;
            lv_StockCheckdataGridView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGridView1_MouseDown);
            lv_StockCheckdataGridView.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.dataGridView_CellFormatting);
            lv_StockCheckdataGridView.Location = new System.Drawing.Point(12, 43);
            lv_StockCheckdataGridView.Name = GridViewName;
            lv_StockCheckdataGridView.ReadOnly = true;
            lv_StockCheckdataGridView.AllowUserToResizeColumns = true;
            lv_StockCheckdataGridView.AllowSorting= false; // this line gives an error since the option doesnt exits
        }
You are working on DataGridView.

Each column has a SortMode property:

you can set:
Col.SortMode = DataGridViewColumnSortMode.NotSortable
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Also you can do this dynamically:


DataGridView DGV;
foreach (DataGridViewColumn C in DGV.Columns)
{
    C.SortMode = DataGridViewColumnSortMode.NotSortable;
}

Open in new window

Commented:
Check DataGridViewColumn.SortMode property.

You can either change it to "NotSortable" in designer, or change it programmatically in intialization.
Jayesh AcharyaTechnichal Consultant

Author

Commented:
For some reason the sort still happens,

Here is what I am doing
        public void BuildGridViewStructure
           (Dictionary<string, string> lv_AllGridCols
           , Dictionary<string, bool> lv_ShowHideGridViewColDictionary
           , Dictionary<string, string> lv_ColumnHeadingTextDictionary
           , Dictionary<string, int> lv_ColumnWidthDictionary
           , Dictionary<string, string> lv_ColumnFormatGridViewDictionary
           , System.Windows.Forms.DataGridView lv_GridView)
        {
            try
            {
                lv_GridView.AutoGenerateColumns = false;

                // Define the total Number of Columns based on all cols in dictionary
                lv_GridView.ColumnCount = lv_AllGridCols.Count;

                //build the datagrid columns programatically
                //and populate the data

                string s;
                int j;
                bool b;

                foreach (KeyValuePair<string, string> AllGridPair in lv_AllGridCols)
                {
                    // associate coloumn with a key
                    s = ""; if (lv_ColumnHeadingTextDictionary.TryGetValue(AllGridPair.Key, out s)) { }
                    lv_GridView.Columns[AllGridPair.Key].Name = s;

                    // column visible
                    b = true; if (lv_ShowHideGridViewColDictionary.TryGetValue(AllGridPair.Key, out b)) { }
                    lv_GridView.Columns[AllGridPair.Key].Visible = b;

                    // column width
                    j = 0; if (lv_ColumnWidthDictionary.TryGetValue(AllGridPair.Key, out j)) { }
                    lv_GridView.Columns[AllGridPair.Key].Width = j;

                    // sorting of columns turned off
                    lv_GridView.Columns[AllGridPair.Key].SortMode = DataGridViewColumnSortMode.NotSortable;
                }
            }
            catch (Exception err) { e.MsgCnslE(err, " ", "BuildGridViewStructure", g_debug_level); }
        }//public void BuildGridViewStructure


Teh dictionaries are contain the cloumn haedings if they are visable etc, and this part works ... so I am assuming that I could apply the same logic when setting the sort on the column .. whiel the code does not fail, it still allows the user to sort ...


I have also tried
        public void BuildGridViewStructure
           (Dictionary<string, string> lv_AllGridCols
           , Dictionary<string, bool> lv_ShowHideGridViewColDictionary
           , Dictionary<string, string> lv_ColumnHeadingTextDictionary
           , Dictionary<string, int> lv_ColumnWidthDictionary
           , Dictionary<string, string> lv_ColumnFormatGridViewDictionary
           , System.Windows.Forms.DataGridView lv_GridView)
        {
            try
            {
                lv_GridView.AutoGenerateColumns = false;

                // Define the total Number of Columns based on all cols in dictionary
                lv_GridView.ColumnCount = lv_AllGridCols.Count;

                //build the datagrid columns programatically
                //and populate the data

                string s;
                int j;
                bool b;

                foreach (KeyValuePair<string, string> AllGridPair in lv_AllGridCols)
                {
                    // associate coloumn with a key
                    s = ""; if (lv_ColumnHeadingTextDictionary.TryGetValue(AllGridPair.Key, out s)) { }
                    lv_GridView.Columns[AllGridPair.Key].Name = s;

                    // column visible
                    b = true; if (lv_ShowHideGridViewColDictionary.TryGetValue(AllGridPair.Key, out b)) { }
                    lv_GridView.Columns[AllGridPair.Key].Visible = b;

                    // column width
                    j = 0; if (lv_ColumnWidthDictionary.TryGetValue(AllGridPair.Key, out j)) { }
                    lv_GridView.Columns[AllGridPair.Key].Width = j;

                    // sorting of columns turned off
                    lv_GridView.Columns[AllGridPair.Key].SortMode = DataGridViewColumnSortMode.NotSortable;
                }

                foreach (DataGridViewColumn C in lv_GridView.Columns)
                {
                    C.SortMode = DataGridViewColumnSortMode.NotSortable;
                }

            }
            catch (Exception err) { e.MsgCnslE(err, " ", "BuildGridViewStructure", g_debug_level); }
        }//public void BuildGridViewStructure

neither seems to stop the sorting
Jayesh AcharyaTechnichal Consultant

Author

Commented:
i did get the solution to work but i had the place the sorting option in the main program
Nice, It would be great if you can show us how you are calling this methods. To solve this issue.
Another solution is to handle AddColumn event, and set SortMode Property inside.

Regards,
Fabián
Jayesh AcharyaTechnichal Consultant

Author

Commented:
thnaks for the help, I am going to try to make the chnages more dynamic so that the admin of the application can have a set up screen to manipulate these types of options.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial