Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在我的DataGridView上进行多次排序?_C# - Fatal编程技术网

C# 如何在我的DataGridView上进行多次排序?

C# 如何在我的DataGridView上进行多次排序?,c#,C#,我正在做一个项目,我需要在单击块按钮后按优先级降序对数据网格进行排序。单击块后,该行中的优先级将从任何数字更改为-1,这将使其移动到网格底部,并将最高优先级值推到网格顶部。它在我第一次单击“阻止”按钮时起作用,但在单击“准备就绪”按钮或“阻止”按钮后,没有其他任何响应。我想用-1值将所有块项目移动到列表的底部,然后在单击“准备就绪”后将所有项目返回到网格中的适当级别。有人能解释一下为什么它只在第一次点击删除按钮时对我的网格进行排序吗 namespace Dispatcher_2._0

我正在做一个项目,我需要在单击块按钮后按优先级降序对数据网格进行排序。单击块后,该行中的优先级将从任何数字更改为-1,这将使其移动到网格底部,并将最高优先级值推到网格顶部。它在我第一次单击“阻止”按钮时起作用,但在单击“准备就绪”按钮或“阻止”按钮后,没有其他任何响应。我想用-1值将所有块项目移动到列表的底部,然后在单击“准备就绪”后将所有项目返回到网格中的适当级别。有人能解释一下为什么它只在第一次点击删除按钮时对我的网格进行排序吗

 namespace Dispatcher_2._0
    {
        public partial class Form1 : Form
        {
            DataTable Table = new DataTable();            

            public Form1()
            {
                InitializeComponent();
            }   

            private void Form1_Load(object sender, EventArgs e)
            {
                Table.Columns.Add("PID", typeof(int));
                Table.Columns.Add("Status", typeof(string));
                Table.Columns.Add("Priority", typeof(int));
                Table.Rows.Add(91, "Current Process", 0);
                Table.Rows.Add(92, "Ready", 23);
                Table.Rows.Add(95, "Ready", 22);
                Table.Rows.Add(93, "Ready", 28);
                Table.Rows.Add(94, "Ready", 44);
                dataGridView1.DataSource = Table;
                DataGridViewButtonColumn Btn = new DataGridViewButtonColumn();
                DataGridViewButtonColumn Btn1 = new DataGridViewButtonColumn();
                DataGridViewButtonColumn Btn2 = new DataGridViewButtonColumn();

                Btn.Name = "Ready";
                Btn.Text = "Ready";

                Btn1.Name = "Block";
                Btn1.Text = "Block";

                Btn2.Name = "Terminate";
                Btn2.Text = "Terminate";
                Btn.UseColumnTextForButtonValue = true;
                Btn1.UseColumnTextForButtonValue = true;
                Btn2.UseColumnTextForButtonValue = true;
                dataGridView1.Columns.Add(Btn);
                dataGridView1.Columns.Add(Btn1);  
                dataGridView1.Columns.Add(Btn2);

            }

            private void button3_Click(object sender, EventArgs e)
            {
                Table.Clear();     
            }

            private void button2_Click(object sender, EventArgs e)
            {
                if (dataGridView1.Rows.Count == 0)
                {
                    Table.Rows.Add(91, "Current Process", 0);
                    Table.Rows.Add(92, "Ready", 23);
                    Table.Rows.Add(95, "Ready", 22);
                    Table.Rows.Add(93, "Ready", 28);
                    Table.Rows.Add(94, "Ready", 44);
                    dataGridView1.DataSource = Table;
                }
                else { }

                dataGridView1.Refresh();
            }

            private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                int selectedRow = dataGridView1.CurrentCell.RowIndex;
                string value = dataGridView1.Rows[0].Cells[1].FormattedValue.ToString();
                string value1 = dataGridView1.Rows[selectedRow].Cells[selectedRow].FormattedValue.ToString();

                if (dataGridView1.Columns[e.ColumnIndex].Name == "Ready")
                {
                    dataGridView1.Rows[selectedRow].Cells[1].Value = "Ready";
                }
                if (dataGridView1.Columns[e.ColumnIndex].Name == "Block")
                {
                    dataGridView1.Rows[selectedRow].Cells[1].Value = "Blocked";
                    dataGridView1.Rows[selectedRow].Cells[2].Value = "-1";
                    DataView Tab = new DataView(Table);
                    Tab.Sort = "Priority desc";
                    dataGridView1.DataSource = Tab;
                }
                if (dataGridView1.Columns[e.ColumnIndex].Name == "Terminate")
                {
                    dataGridView1.Rows.RemoveAt(0);
                }
            }

            private void button1_Click(object sender, EventArgs e)
            {
               Table.Rows.Add(new Random().Next(50, 100), "Ready", textBox1.Text);
            } 
        }
    }

我总是在不使用数据源的情况下简单地填写表格,所以我不确定这是如何工作的。然而,我想我还是看到了您的问题——听起来您需要编写一个自定义比较函数来对表进行排序。每当您有必须基于多列进行排序的数据时,这是必需的。

请看一看哇谢谢,它非常有效,可能会重复