[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Sort Column with Type = CheckBox in DataTable?

Posted on 2009-05-06
4
Medium Priority
?
775 Views
Last Modified: 2012-08-14
Hello,

today I recognized that I can sort all 7 columns in my datagridview but not the column with type checkbox. I want to sort all rows with checkbox set to TRUE.

I found the info that I can not sort the DataTable but only the DataGridView?!

column0 is the column with the CheckBox.

How can I sort the first column with checkbox without destroying my whole code?
public void SetColumnsReadProperty()
        {
            for (int i = 2; i < articlesDataGridView.Columns.Count; i++)
                articlesDataGridView.Columns[i].ReadOnly = true;
             
        }
 
public void BindArticleList(AdsExtendedReader reader)
        {
            if (articlesDataGridView.InvokeRequired)
            {                
                articlesDataGridView.Invoke(uPB, new object[] { reader });
            }
            else
            {
                InitDataGridView();
                dataview.Table.Load(reader);
                articlesDataGridView.DataSource = dataview;
                SetColumnsReadProperty();
            }
        }
 
private void InitDataGridView()
        {
            datatable = new DataTable();
            dataview = new DataView(datatable);
            
            var column0 = new DataColumn { ColumnName = "Artikelfilter", ReadOnly = false, DataType = typeof(Boolean) };           
            datatable.Columns.Add(column0);           
            
            var column1 = new DataColumn { ColumnName = "Punkte", ReadOnly = false, DataType = typeof(int) };
            datatable.Columns.Add(column1);           
 
            articlesDataGridView.AllowUserToResizeRows = false;
            datatable.CaseSensitive = false;
            articlesDataGridView.DataSource = dataview;
            
        }

Open in new window

0
Comment
Question by:MSFanboy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:wht1986
ID: 24321905
instead of column0 being a plain DataColumn can you use DataGridViewCheckBoxColumn

then you can set the sort property
column0.SortMode == DataGridViewColumnSortMode.Automatic;
0
 

Author Comment

by:MSFanboy
ID: 24323838
I did the sort thingie already but it did not work, so I saw your post and tried the DataGridViewCheckBoxColumn suggestion, see code below.


The first Column gets displayed so far but when I set some checkbox to true and sort the new checkboxcolumn it doesnt get sorted ALTHOUGH I set the sortmode to every column to AUTOMATIC in the foreach. 
What can I do else?

private void InitDataGridView()
        {
            datatable = new DataTable();
            dataview = new DataView(datatable);
 
            var columnFilter = new DataGridViewCheckBoxColumn(false); // { ColumnName = "Artikelfilter", ReadOnly = false, DataType = typeof(Boolean) };
           // datatable.Columns.Add(columnFilter)
 
            articlesDataGridView.Columns.Add(columnFilter);
 
            foreach (DataGridViewColumn col in articlesDataGridView.Columns)
            {
                col.SortMode = DataGridViewColumnSortMode.Automatic;
            }
            
            var columnPunkte = new DataColumn { ColumnName = "Punkte", ReadOnly = false, DataType = typeof(int) };
            datatable.Columns.Add(columnPunkte);           
 
            articlesDataGridView.AllowUserToResizeRows = false;
            datatable.CaseSensitive = false;
            articlesDataGridView.DataSource = dataview;
            
        }

Open in new window

0
 
LVL 12

Accepted Solution

by:
wht1986 earned 1500 total points
ID: 24325301
I have an empty DataGridView on my form. In the code below, I create a datasource and assign it to my grid.  The grid is set to auto generate columns.  I manually add the first checkbox column, so I can easily set the properties I want.
public partial class Form1 : Form
{
    protected DataTable datatable = null;
    protected DataView dataview = null;
 
    private void BindArticleList()
    {
        datatable.Rows.Clear();
        datatable.Rows.Add(new object[] { false, 1 });
        datatable.Rows.Add(new object[] { true, 3 });
        datatable.Rows.Add(new object[] { false, 4 });
    }
 
    private void InitDataGridView()
    {
        datatable = new DataTable();
        datatable.Columns.Add(new DataColumn { ColumnName = "Artikelfilter", ReadOnly = false, DataType = typeof(Boolean) });
        datatable.Columns.Add(new DataColumn { ColumnName = "Punkte", ReadOnly = false, DataType = typeof(int) });
        dataview = new DataView(datatable);
 
        DataGridViewCheckBoxColumn dgvCbColumn = new DataGridViewCheckBoxColumn { DataPropertyName = "Artikelfilter", HeaderText = "Artikelfilter", SortMode = DataGridViewColumnSortMode.Automatic };
        articlesDataGridView.Columns.Add(dgvCbColumn);
 
        articlesDataGridView.DataSource = dataview;
    }
 
    private void Form1_Load(object sender, EventArgs e)
    {
        InitDataGridView();
        BindArticleList();
    }
 
    public Form1()
    {
        InitializeComponent();
    }
 
}

Open in new window

0
 

Author Closing Comment

by:MSFanboy
ID: 31578460
that was the code which helped me:

DataGridViewCheckBoxColumn dgvCbColumn = new DataGridViewCheckBoxColumn { DataPropertyName = "Artikelfilter", HeaderText = "Artikelfilter", SortMode = DataGridViewColumnSortMode.Automatic };
        articlesDataGridView.Columns.Add(dgvCbColumn);
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question