Windows窗体中的DataGridveiw [英] DataGridveiw in Windows Forms

查看:118
本文介绍了Windows窗体中的DataGridveiw的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我使用的是Data GridView,其中我一次将数据插入多个单元格中.
为此,我正在使用选定单元格的列索引.
但是列索引从第6列开始.
如何使它从第二列开始

Hello All,

I am using a Data GridView, wherein I insert Data in Multiple Cells at a time.
For this I am Using Column Index of Selected cell.
But the Column Index starts form 6 th Column.
How Can i make it start from the 2nd Column

if(dgvTimeSpan.SelectedCells[index].ColumnIndex != 0 && dgvTimeSpan.SelectedCells[index].RowIndex != 0)
{
                int selectedRow = dgvTimeSpan.SelectedCells[index].RowIndex;
               int selectedColumn = dgvTimeSpan.SelectedCells[index].ColumnIndex;
            curTimeSp.DayCode =  dgvTimeSpan.Columns[selectedColumn].Name.ToString(); curTimeSp.TimeSpNo=int.Parse(dgvTimeSpan.Rows[selectedRow].Cells[1].Value.ToString());
}

推荐答案

搜索索引当然是最快的方法,但我建议您使用列名.
这将使您的代码更具可读性,并且不易出错.
例如,每个程序员在设置currentRow.Cells["colProductName"].Value = someName时都会理解,但是只有很少的人熟悉项目,甚至是那种特定的形式也可以理解currentRow.Cells[3].Value = someName.
使用列名代替索引的另一个很好的理由是,当添加列时,名称不会改变,而索引会改变!
因此,每次添加新列时,您都必须检查代码以查看索引是否仍然正确.

就您的问题而言,请检查是否未在第5个位置(从0开始!)中选择一个单元格,并使用该单元格索引.因此,如果您想更改第6列的值,则可以使用索引5.但是,请再次尝试使用列名.更安全了!
希望对您有所帮助.
While index is of course fastest to search by, may I recommend using the column name.
It will make your code more readable and is less prone to errors.
For example, every programmer will understand when you set currentRow.Cells["colProductName"].Value = someName, but only very few that are familiar with the project, or even that particular form that will understand currentRow.Cells[3].Value = someName.
Another very good reason to use the column name instead of index is that when columns are added the name does not change, but index does!
So every time a new column is added you will have to check your code to see if the indexes are still allright.

As far as your problem goes check if you are not selecting a cell at the 5th position (0-based!) and use that cells index. So if you wanted to change the value of the 6th column you''d use index 5. But again, try column name. It''s a lot safer!
Hope it helps.


这篇关于Windows窗体中的DataGridveiw的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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