你的空白行指的是什么,如果是I个实际的空白行,行号就不会有问题

解决方案 »

  1.   


    就比如说有6行,然后有一行空白行我想要那个空白行的行号,应该是第7行的,但是却一直得到第6行。怎么解决?
      

  2.   

    我实现的代码如下:
    注:不能粘贴内容到新的空白行,只能粘贴到原来有数据的行中,要粘贴到新的空白行的话就粘贴到了最后一行有数据的一行怎么改?????
    private void button4_Click(object sender, EventArgs e)
            {
                try
                {
                    string strSql = "select * from S";
                    DataTable dtshow = new DataTable();
                    dtshow = this.dbconn(strSql);
                    dtshow.Rows.Clear();
                    DataTable dt = new DataTable();
                    dt = (DataTable)this.dataGridView1.DataSource;
                    int cRowIndex = dataGridView1.CurrentCell.RowIndex;
                    int cColIndex = dataGridView1.CurrentCell.ColumnIndex;
                    //最后一行为新行
                    int rowCount = dataGridView1.Rows.Count;
                    int colCount = dataGridView1.ColumnCount;
                    //获取剪贴板内容
                    string pasteText = Clipboard.GetText();
                    //判断是否有字符存在
                    if (string.IsNullOrEmpty(pasteText))
                        return;
                    //以换行符分割的数组
                    string[] lines = pasteText.Trim().Split('\n');
                    int txtLength = lines.Length;
                    DataRow row;
                    //判断是修改还是添加,如果dgv中行数减当前行号大于要粘贴的行数,直接修改
                    if (rowCount - cRowIndex > txtLength)
                    {
                        for (int j = cRowIndex; j < cRowIndex + txtLength; j++)
                        {
                            //以制表符分割的数组
                            string[] vals = lines[j - cRowIndex].Split('\t');
                            //判断要粘贴的列数与dgv中列数减当前列号的大小,取最小值
                            int minColLength = vals.Length > colCount - cColIndex ? colCount - cColIndex : vals.Length;
                            row = dt.Rows[j];
                            for (int i = 0; i < minColLength; i++)
                            {
                                row[i + cColIndex] = vals[i];
                            }
                        }
                    }
                    //否则先修改后添
                    //修改
                    else
                    {
                        for (int j = cRowIndex; j < rowCount; j++)
                        {
                            string[] vals = lines[j - cRowIndex].Split('\t');
                            int minColLength = vals.Length > colCount - cColIndex ? colCount - cColIndex : vals.Length;
                            row = dt.Rows[j];
                            for (int i = 0; i < minColLength; i++)
                            {
                                row[i + cColIndex] = vals[i];
                            }
                        }
                        //添加
                        for (int j = rowCount; j < cRowIndex + txtLength; j++)
                        {
                            string[] vals = lines[j - cRowIndex].Split('\t');
                            int minColLength = vals.Length > colCount - cColIndex ? colCount - cColIndex : vals.Length;
                            //新行
                            row = dt.NewRow();
                            ((DataTable)dataGridView1.DataSource).Rows.Add();
                            for (int i = 0; i < minColLength; i++)
                            {
                                row[i + cColIndex] = vals[i];
                            }
                            //添加到dgv数据源中
                            ((DataTable)dataGridView1.DataSource).Rows.Add(row);
                        }
                        // if (cRowIndex == rowCount)
                        // dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count);
                    }
                }
                catch (Exception MyEx)
                {
                    MessageBox.Show(MyEx.Message);
                }        }