【问题标题】:How to find a row exists in datagridview and append it to the last row of the datagridview如何在datagridview中找到一行并将其附加到datagridview的最后一行
【发布时间】:2010-09-27 14:41:42
【问题描述】:

我有一个 gridview,它将有 2 行,其中一些数据如下

   101 111111111 1111111111009270754A094101// My first row data

  9000002     1000000020222222236000000000000000000000012000000000000000000000000000000000000000//My 2nd row data

我将添加一些值,以便将其插入到这两行之间,并且已经存在的第二行应该移动到数据网格视图的最后一行。像这样,如果我添加 n 个值,则值应该插入到这 2 个值之间,并且应该将已经存在的行移到最后一行,请知道

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    我已经用测试代码编辑了这个:

    public Form1()
            {
    
                InitializeComponent();
                //This can be removed before utilizing
                     dgv.Rows.Add("1", "1", "1");
                     dgv.Rows.Add("1", "1", "1");
                     dgv.Rows.Add("bob", "bob", "bob");
                     dgv.Rows.Add("1", "1", "1");
                     dgv.Rows.Add("1", "1", "1");
                     dgv.Rows.Add("1", "1", "1");
                //This can be removed before utilizing
    
    
                int oldrow = 2;
    
                dgv.Rows.Add(itemArray(dgv.Rows[oldrow]));
    
                dgv.Rows.RemoveAt(oldrow);
                /*
                 DataGridViewRow oldRow = dataGridView1.Rows.Add(itemarray(dataGridView1.Rows[1])); dataGridView1.Rows.Remove(oldRow)
                 */
    
            }
    
            object[] itemArray(DataGridViewRow Row) 
            {
                int a = Row.DataGridView.ColumnCount - 1;
                object[] mOut = new object[a+1];
    
                for (int x = 0;x <= a ; x++)
                {
                    mOut[x] = Row.Cells[x].Value;
                }
                return mOut;
    
            }
    

    对于所有额外的测试,我深表歉意。

    【讨论】:

    • 但我没有得到任何类似的财产
    • 我收到一个错误,因为错误 2 无法将类型“int”隐式转换为“System.Windows.Forms.DataGridViewRow”
    • 请检查以确保该行实际上是一行而不是整数。 DataGridViewRow oldRow = dgv.Rows.Add(itemArray(dgv.Rows[2]));
    • 我写这个 DataGridViewRow oldRow = dataGridView1.Rows.Add(itemarray(dataGridView1.Rows[1])); dataGridView1.Rows.Remove(oldRow);但我得到了那个错误
    • 我将其更改为 int 而不是 datagridview 行,但在函数项数组中,我在 mOut[x] = Row.Cells[x].Value 处出现错误,因为索引超出了数组。
    【解决方案2】:

    这是我写的,但对我不起作用。我需要的是,如果我的数组以 5 开头,我想删除已存在于 gridview 中的第二行,并希望在添加特定行之后附加它

         if (line.StartsWith("5"))
                    {
                        int oldRow = 1;
                        dataGridView1.Rows.Add(itemarray(dataGridView1.Rows[1]));
    
                        dataGridView1.Rows.RemoveAt(oldRow);
    
                        dataGridView1.Rows.Add("BatchHeader", line);
    
                        m_flag = true;
                        StringBuilder sb = new StringBuilder();
                        objfileentry.createFileEntry(Append.FileName, out sb);
                        if (m_flag)
                            dataGridView1.Rows.Add("FileControl", sb.ToString());
                        line = string.Empty;
                    }
    

    你给定的功能

    private object[] itemarray(DataGridViewRow Row)
        {
            int a = Row.DataGridView.ColumnCount - 1;
            object[] mOut = new object[a + 1]; 
    
            for (int x = 0; x <= a; x++)
            {
                mOut[x] = Row.Cells[x].Value;
            }
            return mOut;
    
        }
    

    【讨论】:

    • datagridviewrow 的数据源是什么?
    • 如果我在表单上填写一些值,我不会使用任何数据源,我会通过将值作为字符串数组将其写入网格
    【解决方案3】:

    如果你以这种方式绑定数据,

    myGridView.DataSource = GetDataSource();
    

    您不能以编程方式添加行。如果没有,您可以使用:

    DataGridViewRow newRow = new DataGridViewRow();
    //set row data here
    myGridView.Rows.Insert(newIndex, newRow); //use Insert instead of Add/AddCopy
    

    【讨论】:

      【解决方案4】:

      你能用 ListView 代替 datagrid 吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-14
        • 2020-10-21
        • 1970-01-01
        相关资源
        最近更新 更多