Sort on the basis of CheckBox in C#





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I have a DataGridView in which there are multiple columns, There is a check box Column like this:



enter image description here



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.










share|improve this question

























  • 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


















-1















I have a DataGridView in which there are multiple columns, There is a check box Column like this:



enter image description here



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.










share|improve this question

























  • 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














-1












-1








-1


1






I have a DataGridView in which there are multiple columns, There is a check box Column like this:



enter image description here



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.










share|improve this question
















I have a DataGridView in which there are multiple columns, There is a check box Column like this:



enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 15:07









stuartd

51.7k11100128




51.7k11100128










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 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



















  • 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

















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












1 Answer
1






active

oldest

votes


















0














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;
}


enter image description here






share|improve this answer


























  • 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












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
});


}
});














draft saved

draft discarded


















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









0














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;
}


enter image description here






share|improve this answer


























  • 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
















0














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;
}


enter image description here






share|improve this answer


























  • 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














0












0








0







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;
}


enter image description here






share|improve this answer















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;
}


enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 10:19

























answered Nov 22 '18 at 16:30









Dhanushka DayawanshaDhanushka Dayawansha

351210




351210













  • 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











  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?