如何在DataGridView的任何位置添加新行

如何在DataGridView的任何位置添加新行

本文介绍了如何在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

推荐答案


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



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

08-04 04:33