Sort on the basis of CheckBox in C#
I have a DataGridView in which there are multiple columns, There is a check box Column like this:
I want to sort my datagridview in descending order according to column "weight". But if the checkbox next to weight is checked, that row should go down at the bottom of the list. Finally, I want to have a sorted (in descending order) datagridview according to weight column and I see the unchecked rows first, and then the checked rows. Please help me.
c# winforms
add a comment |
I have a DataGridView in which there are multiple columns, There is a check box Column like this:
I want to sort my datagridview in descending order according to column "weight". But if the checkbox next to weight is checked, that row should go down at the bottom of the list. Finally, I want to have a sorted (in descending order) datagridview according to weight column and I see the unchecked rows first, and then the checked rows. Please help me.
c# winforms
It depends on how you want to do the sort. If you want to use the built-in Sort you can use the overload that takes a comparer, which "allows advanced customization of the sorting feature of the DataGridView class" - basically you need to sort by the checkbox column first, then by the weight column.
– stuartd
Nov 22 '18 at 15:11
in sql you can doorder by [Total Image], Weight desc
which will put all checked rows at the buttom, an sort the rest by weight
– GuidoG
Nov 22 '18 at 15:57
@guidog if sql is used i'd expect this grid to be databound to a datatable, in which case settingdt.DefaultView.Sort="[TotalImage] ASC, [Weight] DESC";
could be used. If the grid is bound to a ibindinglist (such as bindingsource) it could be set there..
– Caius Jard
Nov 22 '18 at 16:07
@CaiusJard Yes, so now he already has 3 options
– GuidoG
Nov 22 '18 at 16:11
add a comment |
I have a DataGridView in which there are multiple columns, There is a check box Column like this:
I want to sort my datagridview in descending order according to column "weight". But if the checkbox next to weight is checked, that row should go down at the bottom of the list. Finally, I want to have a sorted (in descending order) datagridview according to weight column and I see the unchecked rows first, and then the checked rows. Please help me.
c# winforms
I have a DataGridView in which there are multiple columns, There is a check box Column like this:
I want to sort my datagridview in descending order according to column "weight". But if the checkbox next to weight is checked, that row should go down at the bottom of the list. Finally, I want to have a sorted (in descending order) datagridview according to weight column and I see the unchecked rows first, and then the checked rows. Please help me.
c# winforms
c# winforms
edited Nov 22 '18 at 15:07
stuartd
50.9k1098125
50.9k1098125
asked Nov 22 '18 at 15:05
Emad TahmouresEmad Tahmoures
11
11
It depends on how you want to do the sort. If you want to use the built-in Sort you can use the overload that takes a comparer, which "allows advanced customization of the sorting feature of the DataGridView class" - basically you need to sort by the checkbox column first, then by the weight column.
– stuartd
Nov 22 '18 at 15:11
in sql you can doorder by [Total Image], Weight desc
which will put all checked rows at the buttom, an sort the rest by weight
– GuidoG
Nov 22 '18 at 15:57
@guidog if sql is used i'd expect this grid to be databound to a datatable, in which case settingdt.DefaultView.Sort="[TotalImage] ASC, [Weight] DESC";
could be used. If the grid is bound to a ibindinglist (such as bindingsource) it could be set there..
– Caius Jard
Nov 22 '18 at 16:07
@CaiusJard Yes, so now he already has 3 options
– GuidoG
Nov 22 '18 at 16:11
add a comment |
It depends on how you want to do the sort. If you want to use the built-in Sort you can use the overload that takes a comparer, which "allows advanced customization of the sorting feature of the DataGridView class" - basically you need to sort by the checkbox column first, then by the weight column.
– stuartd
Nov 22 '18 at 15:11
in sql you can doorder by [Total Image], Weight desc
which will put all checked rows at the buttom, an sort the rest by weight
– GuidoG
Nov 22 '18 at 15:57
@guidog if sql is used i'd expect this grid to be databound to a datatable, in which case settingdt.DefaultView.Sort="[TotalImage] ASC, [Weight] DESC";
could be used. If the grid is bound to a ibindinglist (such as bindingsource) it could be set there..
– Caius Jard
Nov 22 '18 at 16:07
@CaiusJard Yes, so now he already has 3 options
– GuidoG
Nov 22 '18 at 16:11
It depends on how you want to do the sort. If you want to use the built-in Sort you can use the overload that takes a comparer, which "allows advanced customization of the sorting feature of the DataGridView class" - basically you need to sort by the checkbox column first, then by the weight column.
– stuartd
Nov 22 '18 at 15:11
It depends on how you want to do the sort. If you want to use the built-in Sort you can use the overload that takes a comparer, which "allows advanced customization of the sorting feature of the DataGridView class" - basically you need to sort by the checkbox column first, then by the weight column.
– stuartd
Nov 22 '18 at 15:11
in sql you can do
order by [Total Image], Weight desc
which will put all checked rows at the buttom, an sort the rest by weight– GuidoG
Nov 22 '18 at 15:57
in sql you can do
order by [Total Image], Weight desc
which will put all checked rows at the buttom, an sort the rest by weight– GuidoG
Nov 22 '18 at 15:57
@guidog if sql is used i'd expect this grid to be databound to a datatable, in which case setting
dt.DefaultView.Sort="[TotalImage] ASC, [Weight] DESC";
could be used. If the grid is bound to a ibindinglist (such as bindingsource) it could be set there..– Caius Jard
Nov 22 '18 at 16:07
@guidog if sql is used i'd expect this grid to be databound to a datatable, in which case setting
dt.DefaultView.Sort="[TotalImage] ASC, [Weight] DESC";
could be used. If the grid is bound to a ibindinglist (such as bindingsource) it could be set there..– Caius Jard
Nov 22 '18 at 16:07
@CaiusJard Yes, so now he already has 3 options
– GuidoG
Nov 22 '18 at 16:11
@CaiusJard Yes, so now he already has 3 options
– GuidoG
Nov 22 '18 at 16:11
add a comment |
1 Answer
1
active
oldest
votes
try this
List<DataGridViewRow> newRowcheck = new List<DataGridViewRow>();
List<DataGridViewRow> newRowUncheck = new List<DataGridViewRow>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToBoolean(row.Cells[1].Value) == false)
{
newRowUncheck.Add(row);
}
else
{
newRowcheck.Add(row);
}
}
dataGridView1.Rows.Clear();
int RowIndex;
for (RowIndex = 0; RowIndex < newRowUncheck.Count; RowIndex++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[RowIndex].Cells[0].Value = newRowUncheck[RowIndex].Cells[0].Value;
dataGridView1.Rows[RowIndex].Cells[1].Value = newRowUncheck[RowIndex].Cells[1].Value;
}
dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);
DataGridView tempDataGridView = new DataGridView();
tempDataGridView.Columns.Add("Column1", "Column1 Name in Text");
tempDataGridView.Columns.Add("nColumn2", "Column2 Name in Text");
for (int i = 0; i < newRowcheck.Count; i++)
{
tempDataGridView.Rows.Add();
tempDataGridView.Rows[i].Cells[0].Value = newRowcheck[i].Cells[0].Value;
tempDataGridView.Rows[i].Cells[1].Value = newRowcheck[i].Cells[1].Value;
}
tempDataGridView.Sort(tempDataGridView.Columns[0], ListSortDirection.Descending);
for (int i = 0; i < tempDataGridView.Rows.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i + RowIndex - 1].Cells[0].Value = tempDataGridView.Rows[i].Cells[0].Value;
dataGridView1.Rows[i + RowIndex - 1].Cells[1].Value = tempDataGridView.Rows[i].Cells[1].Value;
}
Thank you very much for your reply. But the problem in this code is in sorting of checked items. This code does not sort checked cells. Could you please help me again?!
– Emad Tahmoures
Nov 23 '18 at 8:41
try my update answer
– Dhanushka Dayawansha
Nov 23 '18 at 10:20
Thank you for your help. Still it does not give the correct answer. I will send you a picture showing the result.
– Emad Tahmoures
Nov 23 '18 at 15:50
is there any error in my code?
– Dhanushka Dayawansha
Nov 23 '18 at 15:52
Unfortunately the final result was not sorted in descending order for checked items. I dont know why!
– Emad Tahmoures
Nov 23 '18 at 16:04
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433727%2fsort-on-the-basis-of-checkbox-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
try this
List<DataGridViewRow> newRowcheck = new List<DataGridViewRow>();
List<DataGridViewRow> newRowUncheck = new List<DataGridViewRow>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToBoolean(row.Cells[1].Value) == false)
{
newRowUncheck.Add(row);
}
else
{
newRowcheck.Add(row);
}
}
dataGridView1.Rows.Clear();
int RowIndex;
for (RowIndex = 0; RowIndex < newRowUncheck.Count; RowIndex++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[RowIndex].Cells[0].Value = newRowUncheck[RowIndex].Cells[0].Value;
dataGridView1.Rows[RowIndex].Cells[1].Value = newRowUncheck[RowIndex].Cells[1].Value;
}
dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);
DataGridView tempDataGridView = new DataGridView();
tempDataGridView.Columns.Add("Column1", "Column1 Name in Text");
tempDataGridView.Columns.Add("nColumn2", "Column2 Name in Text");
for (int i = 0; i < newRowcheck.Count; i++)
{
tempDataGridView.Rows.Add();
tempDataGridView.Rows[i].Cells[0].Value = newRowcheck[i].Cells[0].Value;
tempDataGridView.Rows[i].Cells[1].Value = newRowcheck[i].Cells[1].Value;
}
tempDataGridView.Sort(tempDataGridView.Columns[0], ListSortDirection.Descending);
for (int i = 0; i < tempDataGridView.Rows.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i + RowIndex - 1].Cells[0].Value = tempDataGridView.Rows[i].Cells[0].Value;
dataGridView1.Rows[i + RowIndex - 1].Cells[1].Value = tempDataGridView.Rows[i].Cells[1].Value;
}
Thank you very much for your reply. But the problem in this code is in sorting of checked items. This code does not sort checked cells. Could you please help me again?!
– Emad Tahmoures
Nov 23 '18 at 8:41
try my update answer
– Dhanushka Dayawansha
Nov 23 '18 at 10:20
Thank you for your help. Still it does not give the correct answer. I will send you a picture showing the result.
– Emad Tahmoures
Nov 23 '18 at 15:50
is there any error in my code?
– Dhanushka Dayawansha
Nov 23 '18 at 15:52
Unfortunately the final result was not sorted in descending order for checked items. I dont know why!
– Emad Tahmoures
Nov 23 '18 at 16:04
|
show 3 more comments
try this
List<DataGridViewRow> newRowcheck = new List<DataGridViewRow>();
List<DataGridViewRow> newRowUncheck = new List<DataGridViewRow>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToBoolean(row.Cells[1].Value) == false)
{
newRowUncheck.Add(row);
}
else
{
newRowcheck.Add(row);
}
}
dataGridView1.Rows.Clear();
int RowIndex;
for (RowIndex = 0; RowIndex < newRowUncheck.Count; RowIndex++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[RowIndex].Cells[0].Value = newRowUncheck[RowIndex].Cells[0].Value;
dataGridView1.Rows[RowIndex].Cells[1].Value = newRowUncheck[RowIndex].Cells[1].Value;
}
dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);
DataGridView tempDataGridView = new DataGridView();
tempDataGridView.Columns.Add("Column1", "Column1 Name in Text");
tempDataGridView.Columns.Add("nColumn2", "Column2 Name in Text");
for (int i = 0; i < newRowcheck.Count; i++)
{
tempDataGridView.Rows.Add();
tempDataGridView.Rows[i].Cells[0].Value = newRowcheck[i].Cells[0].Value;
tempDataGridView.Rows[i].Cells[1].Value = newRowcheck[i].Cells[1].Value;
}
tempDataGridView.Sort(tempDataGridView.Columns[0], ListSortDirection.Descending);
for (int i = 0; i < tempDataGridView.Rows.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i + RowIndex - 1].Cells[0].Value = tempDataGridView.Rows[i].Cells[0].Value;
dataGridView1.Rows[i + RowIndex - 1].Cells[1].Value = tempDataGridView.Rows[i].Cells[1].Value;
}
Thank you very much for your reply. But the problem in this code is in sorting of checked items. This code does not sort checked cells. Could you please help me again?!
– Emad Tahmoures
Nov 23 '18 at 8:41
try my update answer
– Dhanushka Dayawansha
Nov 23 '18 at 10:20
Thank you for your help. Still it does not give the correct answer. I will send you a picture showing the result.
– Emad Tahmoures
Nov 23 '18 at 15:50
is there any error in my code?
– Dhanushka Dayawansha
Nov 23 '18 at 15:52
Unfortunately the final result was not sorted in descending order for checked items. I dont know why!
– Emad Tahmoures
Nov 23 '18 at 16:04
|
show 3 more comments
try this
List<DataGridViewRow> newRowcheck = new List<DataGridViewRow>();
List<DataGridViewRow> newRowUncheck = new List<DataGridViewRow>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToBoolean(row.Cells[1].Value) == false)
{
newRowUncheck.Add(row);
}
else
{
newRowcheck.Add(row);
}
}
dataGridView1.Rows.Clear();
int RowIndex;
for (RowIndex = 0; RowIndex < newRowUncheck.Count; RowIndex++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[RowIndex].Cells[0].Value = newRowUncheck[RowIndex].Cells[0].Value;
dataGridView1.Rows[RowIndex].Cells[1].Value = newRowUncheck[RowIndex].Cells[1].Value;
}
dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);
DataGridView tempDataGridView = new DataGridView();
tempDataGridView.Columns.Add("Column1", "Column1 Name in Text");
tempDataGridView.Columns.Add("nColumn2", "Column2 Name in Text");
for (int i = 0; i < newRowcheck.Count; i++)
{
tempDataGridView.Rows.Add();
tempDataGridView.Rows[i].Cells[0].Value = newRowcheck[i].Cells[0].Value;
tempDataGridView.Rows[i].Cells[1].Value = newRowcheck[i].Cells[1].Value;
}
tempDataGridView.Sort(tempDataGridView.Columns[0], ListSortDirection.Descending);
for (int i = 0; i < tempDataGridView.Rows.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i + RowIndex - 1].Cells[0].Value = tempDataGridView.Rows[i].Cells[0].Value;
dataGridView1.Rows[i + RowIndex - 1].Cells[1].Value = tempDataGridView.Rows[i].Cells[1].Value;
}
try this
List<DataGridViewRow> newRowcheck = new List<DataGridViewRow>();
List<DataGridViewRow> newRowUncheck = new List<DataGridViewRow>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToBoolean(row.Cells[1].Value) == false)
{
newRowUncheck.Add(row);
}
else
{
newRowcheck.Add(row);
}
}
dataGridView1.Rows.Clear();
int RowIndex;
for (RowIndex = 0; RowIndex < newRowUncheck.Count; RowIndex++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[RowIndex].Cells[0].Value = newRowUncheck[RowIndex].Cells[0].Value;
dataGridView1.Rows[RowIndex].Cells[1].Value = newRowUncheck[RowIndex].Cells[1].Value;
}
dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);
DataGridView tempDataGridView = new DataGridView();
tempDataGridView.Columns.Add("Column1", "Column1 Name in Text");
tempDataGridView.Columns.Add("nColumn2", "Column2 Name in Text");
for (int i = 0; i < newRowcheck.Count; i++)
{
tempDataGridView.Rows.Add();
tempDataGridView.Rows[i].Cells[0].Value = newRowcheck[i].Cells[0].Value;
tempDataGridView.Rows[i].Cells[1].Value = newRowcheck[i].Cells[1].Value;
}
tempDataGridView.Sort(tempDataGridView.Columns[0], ListSortDirection.Descending);
for (int i = 0; i < tempDataGridView.Rows.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i + RowIndex - 1].Cells[0].Value = tempDataGridView.Rows[i].Cells[0].Value;
dataGridView1.Rows[i + RowIndex - 1].Cells[1].Value = tempDataGridView.Rows[i].Cells[1].Value;
}
edited Nov 23 '18 at 10:19
answered Nov 22 '18 at 16:30
Dhanushka DayawanshaDhanushka Dayawansha
32619
32619
Thank you very much for your reply. But the problem in this code is in sorting of checked items. This code does not sort checked cells. Could you please help me again?!
– Emad Tahmoures
Nov 23 '18 at 8:41
try my update answer
– Dhanushka Dayawansha
Nov 23 '18 at 10:20
Thank you for your help. Still it does not give the correct answer. I will send you a picture showing the result.
– Emad Tahmoures
Nov 23 '18 at 15:50
is there any error in my code?
– Dhanushka Dayawansha
Nov 23 '18 at 15:52
Unfortunately the final result was not sorted in descending order for checked items. I dont know why!
– Emad Tahmoures
Nov 23 '18 at 16:04
|
show 3 more comments
Thank you very much for your reply. But the problem in this code is in sorting of checked items. This code does not sort checked cells. Could you please help me again?!
– Emad Tahmoures
Nov 23 '18 at 8:41
try my update answer
– Dhanushka Dayawansha
Nov 23 '18 at 10:20
Thank you for your help. Still it does not give the correct answer. I will send you a picture showing the result.
– Emad Tahmoures
Nov 23 '18 at 15:50
is there any error in my code?
– Dhanushka Dayawansha
Nov 23 '18 at 15:52
Unfortunately the final result was not sorted in descending order for checked items. I dont know why!
– Emad Tahmoures
Nov 23 '18 at 16:04
Thank you very much for your reply. But the problem in this code is in sorting of checked items. This code does not sort checked cells. Could you please help me again?!
– Emad Tahmoures
Nov 23 '18 at 8:41
Thank you very much for your reply. But the problem in this code is in sorting of checked items. This code does not sort checked cells. Could you please help me again?!
– Emad Tahmoures
Nov 23 '18 at 8:41
try my update answer
– Dhanushka Dayawansha
Nov 23 '18 at 10:20
try my update answer
– Dhanushka Dayawansha
Nov 23 '18 at 10:20
Thank you for your help. Still it does not give the correct answer. I will send you a picture showing the result.
– Emad Tahmoures
Nov 23 '18 at 15:50
Thank you for your help. Still it does not give the correct answer. I will send you a picture showing the result.
– Emad Tahmoures
Nov 23 '18 at 15:50
is there any error in my code?
– Dhanushka Dayawansha
Nov 23 '18 at 15:52
is there any error in my code?
– Dhanushka Dayawansha
Nov 23 '18 at 15:52
Unfortunately the final result was not sorted in descending order for checked items. I dont know why!
– Emad Tahmoures
Nov 23 '18 at 16:04
Unfortunately the final result was not sorted in descending order for checked items. I dont know why!
– Emad Tahmoures
Nov 23 '18 at 16:04
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433727%2fsort-on-the-basis-of-checkbox-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It depends on how you want to do the sort. If you want to use the built-in Sort you can use the overload that takes a comparer, which "allows advanced customization of the sorting feature of the DataGridView class" - basically you need to sort by the checkbox column first, then by the weight column.
– stuartd
Nov 22 '18 at 15:11
in sql you can do
order by [Total Image], Weight desc
which will put all checked rows at the buttom, an sort the rest by weight– GuidoG
Nov 22 '18 at 15:57
@guidog if sql is used i'd expect this grid to be databound to a datatable, in which case setting
dt.DefaultView.Sort="[TotalImage] ASC, [Weight] DESC";
could be used. If the grid is bound to a ibindinglist (such as bindingsource) it could be set there..– Caius Jard
Nov 22 '18 at 16:07
@CaiusJard Yes, so now he already has 3 options
– GuidoG
Nov 22 '18 at 16:11