如何在DataGridView的任何位置添加新行? [英] How to add new Row anywhere on the DataGridView ?

查看:187
本文介绍了如何在DataGridView的任何位置添加新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在指定位置(而不是DataGridView的末尾)添加新行?
新行将添加到GridView的末尾.
我想在要添加的位置添加新行.
在Visual Studio 2008中
使用c#3.5

How can a new row in the specified location other than the end of the DataGridView added ?
The new row is added at the end of the GridView.
I want the new row wherever I wanted to add.
in Visual studio 2008
with c# 3.5

推荐答案

看看DataGridViewRowCollection
Take a look at the DataGridViewRowCollection methods[^] and see if one of the Insert() methods will do what you want. For example, "Insert(Int32, Int32) Inserts the specified number of rows into the collection at the specified location."


感谢您的好提示
这样的代码已经解决了我的问题
我的此类代码问题已解决.
Thank you for your good tips
I''ve solved my issue such a code
My issue with code like this got resolved.
<br />
<br />
<pre lang="msil">private void AddRows(params DataGridViewRow[] rows)<br />
        {<br />
            InsertRows(dataGridView1.CurrentRow.Index , rows);<br />
        }<br />
        // Workaround for bug that prevents DataGridViewRowCollection.InsertRange<br />
        // from working when any rows before the insertion index are selected.<br />
        private void InsertRows(int index, params DataGridViewRow[] rows)<br />
        {<br />
            System.Collections.Generic.List<int> selectedIndexes =<br />
                new System.Collections.Generic.List<int>();<br />
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)<br />
            {<br />
                if (row.Index >= index)<br />
                {<br />
                    selectedIndexes.Add(row.Index);<br />
                    row.Selected = false;<br />
                }<br />
            }<br />
            dataGridView1.Rows.InsertRange(index, rows);<br />
            foreach (int selectedIndex in selectedIndexes)<br />
            {<br />
                dataGridView1.Rows[selectedIndex].Selected = true;<br />
            }<br />
        }<br />
        private void button1_Click(object sender, EventArgs e)<br />
        {<br />
            DataGridViewRow row = new DataGridViewRow();<br />
            AddRows(row);<br />
<br />
        }<br />
</pre><br />
<br />
<br />


简短的答案是-您不能.

长答案取决于在这样的Q/A论坛中无法令人满意地回答的太多事情.

如果将DataGridview绑定到数据集,则可以独立于DGV向数据集添加新记录,并且如果对索引进行了有利的设置,则新行将出现在DGV中的正确位置.
The short answer is - you can''t.

The long answer depends on too many things to be satisfactorily answered in a Q/A forum like this.

If your DataGridview is bound to a DataSet you can add a new record to the dataset, independently from the DGV, and if your index(es) are set favourably the new row will appear in its correct place in the DGV.


这篇关于如何在DataGridView的任何位置添加新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆