你的空白行指的是什么,如果是I个实际的空白行,行号就不会有问题
解决方案 »
- Invoke类型的已垃圾回收委托进行了回调,这可能会导致应用程序崩溃、损坏和数据丢失求大神指导!
- rdlc报表的数据集字段的设置问题
- 连接不上远程SQL2005,真是欲哭无泪啊!!
- treeview数据绑定问题
- picturebox的问题, 寻求高手指点
- C#,Winform中为什么设置DataGridViewComboBoxColumn.DisplayMember时会提示出错"未将对象引用设置到对象的实例"
- [没有注册接口 200分] Web安装项目编译过程中出现这样的怪事!
- 急问关于一个DATASET的问题
- 有关图像识别技术的问题
- CType(object_a, Form)
- linq分组统计
- C# where(=>) 语句的意义和使用
就比如说有6行,然后有一行空白行我想要那个空白行的行号,应该是第7行的,但是却一直得到第6行。怎么解决?
注:不能粘贴内容到新的空白行,只能粘贴到原来有数据的行中,要粘贴到新的空白行的话就粘贴到了最后一行有数据的一行怎么改?????
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);
} }