Click here to Skip to main content
15,898,538 members
Home / Discussions / C#
   

C#

 
GeneralDateTime Pin
Bahadir Cambel14-May-05 9:15
Bahadir Cambel14-May-05 9:15 
GeneralRe: DateTime Pin
quilkin14-May-05 10:34
quilkin14-May-05 10:34 
GeneralRe: DateTime Pin
Luis Alonso Ramos14-May-05 17:13
Luis Alonso Ramos14-May-05 17:13 
GeneralRe: DateTime Pin
pubududilena15-May-05 18:15
pubududilena15-May-05 18:15 
GeneralNewbie: Q regarding Sorting DataGrid Pin
nad226314-May-05 8:36
nad226314-May-05 8:36 
GeneralRe: Newbie: Q regarding Sorting DataGrid Pin
MoustafaS14-May-05 14:41
MoustafaS14-May-05 14:41 
GeneralRe: Newbie: Q regarding Sorting DataGrid Pin
nad226314-May-05 18:21
nad226314-May-05 18:21 
GeneralRe: Newbie: Q regarding Sorting DataGrid Pin
Gary Perkin17-May-05 5:58
Gary Perkin17-May-05 5:58 
Hi,

I have a possible solution for you - it has a couple of drawbacks, but might help.

This example assumes that you have a DataGrid called dataGrid1, who's DataSource is a DataTable called dt.
It also assumes that you've declared an ArrayList called al.

First of all, set the "AllowSorting" property of dataGrid1 to "false" - you have to sort it manually.

Then add the following event handler code (don't forget to wire it up to dataGrid1's DataGrid1MouseDown event in the Properties Grid).

void DataGrid1MouseDown(object sender,
                        System.Windows.Forms.MouseEventArgs e)
{
    //Determine where we clicked the DataGrid
    Point pt = new Point(e.X, e.Y);
    DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);

    if(e.Button == MouseButtons.Left)
    {
        // If it was a LeftClick on a Column Header...
        if (hti.Type == DataGrid.HitTestType.ColumnHeader)
        {
            // Determine the column name
            DataColumn dc;
            dc = dt.Columns[hti.Column];

            // Save the data in column1 of any selected row in an array.
            al = new ArrayList();
            for(int i = 0; i < dt.Rows.Count; i++)
            {
                if (dataGrid1.IsSelected(i))
                {
                    listBox1.Items.Add(dt.Rows[i][0].ToString());
                    al.Add(dt.Rows[i][0].ToString());
                }
            }
            SortData(dc.ColumnName);
        }
    }
}


This has basically saved the column1 contents of each selected row in an array, so we can later "re-select" the correct rows.

Now add the following code for the SortData method.

private void SortData(string colname)
{
    // Sort the DataTable into an array
    DataRow[] drarray;
    drarray = dt.Select(null, colname,
                        DataViewRowState.CurrentRows);

    int len = drarray.Length;

    // Add the rows from the sorted array to the end of the table
    for (int i = 0 ; i < drarray.Length ; i++)
    {
        DataRow dr = dt.NewRow();
        for (int j = 0; j < dt.Columns.Count; j++)
        {
            dr[j] = drarray[i][j];
        }
        dt.Rows.Add(dr);
    }

    // Remove the original rows
    for (int i = len - 1 ; i >=0  ; i--)
    {
        dt.Rows.RemoveAt(i);
    }

    // If the data in column1 of any row is in the ArrayList,
    // then Select the corresponding DataGrid row.
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        if (al.Contains(dt.Rows[i][0].ToString()))
        {
            dataGrid1.Select(i);
        }
    }
}


This sorts the original DataTable into an array, adds the sorted records to the end of the DataTable, then deletes the original ones.
It then checks each row in the DataTable, and if the data in column1 matches the saved data in the ArrayList, it marks the corresponding rows in the DataGrid as "Selected".

Therefore, multiple selected rows are supported.

Limitations:

1. It only supports ascending sorts.
You might want to build a better sort using an IComparer
interface.
2. You lose the little arrows on the column headers.
3. It assumes that the data in Column1 of the DataTable is unique.
You could combine data from more than one column to create a
unique key.

Hopefully this will help get on the right track.
Good luck!

Gary Perkin.

PS I think the Sort Event that the previous respondent was referring to is available in WebForms, but I may be wrong...
GeneralRe: Newbie: Q regarding Sorting DataGrid Pin
nad226318-May-05 10:39
nad226318-May-05 10:39 
Generali have the same problem also Pin
snouto14-May-05 8:00
snouto14-May-05 8:00 
GeneralRe: i have the same problem also Pin
Polis Pilavas14-May-05 8:10
Polis Pilavas14-May-05 8:10 
GeneralSmall question Pin
snouto14-May-05 7:42
snouto14-May-05 7:42 
GeneralRe: Small question Pin
Polis Pilavas14-May-05 8:01
Polis Pilavas14-May-05 8:01 
GeneralRe: Small question Pin
snouto14-May-05 8:11
snouto14-May-05 8:11 
GeneralRe: Small question Pin
Polis Pilavas14-May-05 8:14
Polis Pilavas14-May-05 8:14 
GeneralRe: Small question Pin
snouto14-May-05 8:19
snouto14-May-05 8:19 
GeneralRe: Small question Pin
MoustafaS14-May-05 10:28
MoustafaS14-May-05 10:28 
GeneralRe: Small question Pin
mav.northwind14-May-05 20:25
mav.northwind14-May-05 20:25 
GeneralRe: Small question Pin
lgstef16-May-05 1:31
lgstef16-May-05 1:31 
GeneralAsynchronous file transfer client/server Pin
methodincharge14-May-05 7:37
methodincharge14-May-05 7:37 
GeneralRe: Asynchronous file transfer client/server Pin
snouto14-May-05 7:49
snouto14-May-05 7:49 
GeneralCatching event before file opens Pin
krisst_k14-May-05 3:43
krisst_k14-May-05 3:43 
GeneralRe: Catching event before file opens Pin
MoustafaS14-May-05 7:10
MoustafaS14-May-05 7:10 
GeneralRe: Catching event before file opens Pin
krisst_k14-May-05 14:23
krisst_k14-May-05 14:23 
GeneralRe: Catching event before file opens Pin
Dave Kreskowiak15-May-05 5:51
mveDave Kreskowiak15-May-05 5:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.