Click here to Skip to main content
15,877,482 members
Home / Discussions / C#
   

C#

 
AnswerRe: A question of microoptimization Pin
Dave Kreskowiak3-Apr-21 5:50
mveDave Kreskowiak3-Apr-21 5:50 
GeneralRe: A question of microoptimization Pin
Jörgen Andersson3-Apr-21 5:56
professionalJörgen Andersson3-Apr-21 5:56 
QuestionHow to remove rows of a DataTable based on checkboxes? Pin
Alex Dunlop1-Apr-21 8:47
Alex Dunlop1-Apr-21 8:47 
AnswerRe: How to remove rows of a DataTable based on checkboxes? Pin
OriginalGriff1-Apr-21 9:33
mveOriginalGriff1-Apr-21 9:33 
GeneralRe: How to remove rows of a DataTable based on checkboxes? Pin
Alex Dunlop1-Apr-21 18:11
Alex Dunlop1-Apr-21 18:11 
GeneralRe: How to remove rows of a DataTable based on checkboxes? Pin
jschell9-Apr-21 14:59
jschell9-Apr-21 14:59 
GeneralRe: How to remove rows of a DataTable based on checkboxes? Pin
Alex Dunlop1-Apr-21 18:24
Alex Dunlop1-Apr-21 18:24 
GeneralRe: How to remove rows of a DataTable based on checkboxes? Pin
Alex Dunlop1-Apr-21 18:28
Alex Dunlop1-Apr-21 18:28 
My code may seem complicated but I try to give you some necessary parts. In this code, I delete rows based on duplicate contents of column E, from/to dates and toggle checkboxes checked:

C#
int LastRow = my_table3.Rows.Count;
            //Joining year and month to create date format
            my_table3.Columns.Add("تاریخ");
            my_table3.Rows[0][35] = "تاریخ";
            for (int i = 1; i < LastRow; i++)
            {
                my_table3.Rows[i][35] = string.Join("/", my_table3.Rows[i][19], my_table3.Rows[i][20]);
            }

            //Adding checked farm items into a list
            var check_box = new List<string>();
            foreach (BarItem c in ribbonControl1.Items)
            {
                if (c is BarToggleSwitchItem tg)
                {
                    if (tg.Checked)
                    {
                        check_box.Add(tg.Description);
                    }
                }
            }

            //Let's keep track of every value seen as we go through the rows

            var valuesSeen = new HashSet<string>();
            //Rows marked for deletion
            var duplicateRows = new List<int>();

            column_select.EditValue = "E";
            string selcolumn = (string)column_select.EditValue;

            int column_index = worksheet.Columns[selcolumn].Index;
            for (int i = 0; i < LastRow; i++)
            {
                string cellValue = my_table3.Rows[i][column_index].ToString();
                //Returns false if the value already exists
                if (valuesSeen.Add(cellValue) == false)
                {
                    //Mark this row for deletion since we've seen the value before
                    duplicateRows.Add(i);
                }
            }

            //Delete all marked rows only after we're done identifying them.
            for (int i = duplicateRows.Count - 1; i >= 0; i--)
            {
                int index = duplicateRows[i];
                my_table3.Rows[index].Delete();
                //Deleted rows get emty space in table, so we neeed to delete cpmpletely: 
                my_table3.AcceptChanges();
            }
            LastRow = my_table3.Rows.Count;
            var ToDelete = new List<int>();
            for (int i = 1; i < LastRow; i++)
            {
                //Comparing dates
                string one = FromDate.EditValue.ToString();
                string two = ToDate.EditValue.ToString();
                string three = my_table3.Rows[i][35].ToString();
                DateTime dOne = GetDate(one);
                DateTime dTwo = GetDate(two);
                DateTime dThree = GetDate(three);
                bool answer = dThree >= dOne && dThree <= dTwo;

                string cellValue = my_table3.Rows[i][10].ToString();
                string cellvalue2 = my_table3.Rows[i][9].ToString();

                for (int k = 0; k < check_box.Count; k++)
                {
                    if (cellValue != "خاتمه یافته" || cellvalue2 != "تعمیراتی" || answer == false || my_table3.Rows[i][14].ToString() != check_box[k].ToString())
                    {
                        ToDelete.Add(i);
                    }
                }
            }
            for (int i = ToDelete.Count - 1; i >= 0; i--)
            {
                int index = ToDelete[i];
                my_table3.Rows[index].Delete();
                //Deleted rows get empty space in table, so we need to delete completely: 
                my_table3.AcceptChanges();
            }


modified 2-Apr-21 1:39am.

AnswerRe: How to remove rows of a DataTable based on checkboxes? Pin
Mycroft Holmes1-Apr-21 13:58
professionalMycroft Holmes1-Apr-21 13:58 
GeneralRe: How to remove rows of a DataTable based on checkboxes? Pin
Alex Dunlop1-Apr-21 18:12
Alex Dunlop1-Apr-21 18:12 
GeneralRe: How to remove rows of a DataTable based on checkboxes? Pin
Mycroft Holmes2-Apr-21 13:18
professionalMycroft Holmes2-Apr-21 13:18 
AnswerRe: How to remove rows of a DataTable based on checkboxes? Pin
Gerry Schmitz1-Apr-21 18:28
mveGerry Schmitz1-Apr-21 18:28 
GeneralRe: How to remove rows of a DataTable based on checkboxes? Pin
Alex Dunlop1-Apr-21 18:32
Alex Dunlop1-Apr-21 18:32 
GeneralRe: How to remove rows of a DataTable based on checkboxes? Pin
Gerry Schmitz1-Apr-21 19:39
mveGerry Schmitz1-Apr-21 19:39 
QuestionPartial View Question Pin
pkfox31-Mar-21 8:39
professionalpkfox31-Mar-21 8:39 
AnswerRe: Partial View Question Pin
Matthew Dennis31-Mar-21 11:04
sysadminMatthew Dennis31-Mar-21 11:04 
GeneralRe: Partial View Question Pin
pkfox31-Mar-21 21:13
professionalpkfox31-Mar-21 21:13 
QuestionHow autoconnect shapes in Canvas WPF Pin
Newbee_Mark30-Mar-21 23:54
Newbee_Mark30-Mar-21 23:54 
AnswerRe: How autoconnect shapes in Canvas WPF Pin
Gerry Schmitz31-Mar-21 7:33
mveGerry Schmitz31-Mar-21 7:33 
GeneralRe: How autoconnect shapes in Canvas WPF Pin
Newbee_Mark31-Mar-21 7:37
Newbee_Mark31-Mar-21 7:37 
GeneralRe: How autoconnect shapes in Canvas WPF Pin
Gerry Schmitz31-Mar-21 14:57
mveGerry Schmitz31-Mar-21 14:57 
GeneralRe: How autoconnect shapes in Canvas WPF Pin
Newbee_Mark31-Mar-21 23:40
Newbee_Mark31-Mar-21 23:40 
GeneralRe: How autoconnect shapes in Canvas WPF Pin
Gerry Schmitz1-Apr-21 6:24
mveGerry Schmitz1-Apr-21 6:24 
GeneralRe: How autoconnect shapes in Canvas WPF Pin
Newbee_Mark1-Apr-21 22:54
Newbee_Mark1-Apr-21 22:54 
QuestionList not printing out hardcoded books already added to list Pin
Member 1451443230-Mar-21 12:14
Member 1451443230-Mar-21 12:14 

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.