日期:2014-05-20  浏览次数:20767 次

datagridview控件如何同时选多条记录
datagridview控件如何同时选多条记录呢, 我做一个窗体FORM1 ,和一个窗体FORM2 ,每个窗体都有datagridview控件,我想在FORM1上的datagridview上同时选择5条记录,复制到FORM2控件上,这个例子有没有, 我想左键呢是选择,右键是放弃所有的选择,高人们,帮我看一下这个代码如何写,非常感谢. 冬天太冷,多穿衣服.

------解决方案--------------------
让同时选多条记录:设置dataGridView1.MultiSelect = true;
复制多行:
List<model> modelList = new List<model>(); //存储复制的model(行)
void toolStripMenuItemCopy_Click(object sender, EventArgs e)
{
modelList.Clear();
if (dataGridView1.SelectedRows.Count > 0)
{
toolStripMenuItemPaste.Enabled = true;
toolStripMenuItemCopyEnabled = false;
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
{
model modelTemp = new model(); 
tool.gridviewtomodel.Datagridview2Model(dataGridView1, modelTemp, Convert.ToInt32(dataGridView1.SelectedRows[i].Index));
modelTemp.字段= this.字段;
modelList.Add(modelTemp);
}
//设置此行的状态
foreach (DataGridViewRow dgvRow in dataGridView1.SelectedRows)
{ //复制时不用移除,剪切需要移除行
dataGridView1.Rows.Remove(dgvRow);

}
}
//黏贴
 void toolStripMenuItemPaste_Click(object sender, EventArgs e)
{
toolStripMenuItemCopy.Enabled = true;
toolStripMenuItemPaste.Enabled = false;

foreach (model modelTemp in modelList)
{
modelTemp.字段=字段;
ws_model.Update(modelTemp); //做更新操作
}
modelList.Clear();
}
------解决方案--------------------
1、dataGridView1.MultiSelect = true;
2、在后台遍历并获取选中的多行,作为一个数据集传到form2中,作为form2的数据源即可
------解决方案--------------------
C# code

//下面的代码 左键点击增加选择 右键取消选择,不知道有没有更好的办法

private List<int> selectedRowsIndex = new List<int>();//用来存放选中的index
        delegate void SetSelectState(bool deSelect, int rowIndex);

        void RefreshTheSelectedStates(bool deSelect, int rowIndex)
        {
            try
            {
                foreach (int idx in selectedRowsIndex)
                {
                    dataGridView2.Rows[idx].Selected = true;
                }
                if (deSelect)
                {
                    dataGridView2.Rows[rowIndex].Selected = false;
                }
            }
            finally
            {
                dataGridView1.Refresh();
            }
        }

        private void dataGridView2_MouseDown(object sender, MouseEventArgs e)
        {
            DataGridView.HitTestInfo testInfo = dataGridView2.HitTest(e.X, e.Y);
            int rowIdx = testInfo.RowIndex;

            switch(e.Button)
            {
                case MouseButtons.Left:

                    if (rowIdx >= 0)
                    {
                        if (!selectedRowsIndex.Contains(rowIdx))
                            selectedRowsIndex.Add(rowIdx);

                        dataGridView2.BeginInvoke(new SetSelectState(RefreshTheSelectedStates), false, 0);

                    }
                    break;

                case MouseButtons.Right:
                    if (rowIdx >= 0)
                    {
                        if (selectedRowsIndex.Contains(rowIdx))
                            selectedRowsIndex.Remove(rowIdx);

                        dataGridView2.BeginInvoke(new SetSelectState(RefreshTheSelectedStates), true, rowIdx);
                    }

                    break;
            }

        }

------解决方案--------------------