为什么我的datagridview没有选择第一行? [英] Why does my datagridview not select th first row ?
问题描述
我的Windows窗体应用程序中有一个数据网格视图,里面装满了数据,我点击了(nextButton)来遍历网格中的每一行。当我加载我的表格时,似乎每当我点击下一个按钮时,选择从第二行而不是第一行开始..为什么会这样?有什么我可以做的吗?
我的datagridview代码
Hi,
I have got a datagridview in my windows forms app, which is filled up with data, I have (nextButton) that is clicked to go through each and every row in the Grid. When ever I load my form its seems that whenever I click the next button the selection starts from the second row and not the first row.. why is this? is there anything I can do?
My code for datagridview
SqlConnection conn = new SqlConnection(connection);
SqlCommand db = new SqlCommand("select * from TblEmp where Emp_Title = 'Mr'", conn);
SqlCommandBuilder builder = new SqlCommandBuilder(dataAdapter);
dataAdapter = new SqlDataAdapter(db);
dataset = new DataSet();
dataAdapter.Fill(dataset, "TblEmp");
dataGridView1.DataSource = dataset.Tables["TblEmp"];
nextButton的mycode
Mycode for nextButton
void NextRecord()
{
if ( RecordCounter < dataset.Tables[0].Rows.Count -1)
{
RecordCounter++;
TxtDisplayQuestion.Text = dataset.Tables[0].Rows[ RecordCounter]["Emp_Title"].ToString();
}
谢谢你
Thank you
推荐答案
使用你的 RecordCounter ++;在TxtDisplayQuestion.Text = ...代码行之后。
use your "RecordCounter++;" after the "TxtDisplayQuestion.Text = ..." code line.
不知道你的架构,所以试一试..
Don't know about your schema, so give it a try..
void NextRecord()
{
if ( RecordCounter < dataset.Tables[0].Rows.Count -1)
{
TxtDisplayQuestion.Text = dataset.Tables[0].Rows[ RecordCounter]["Emp_Title"].ToString();
}
RecordCounter++;
}
-KR
-KR
//as first
void NextRecord()
{
if ( RecordCounter < dataset.Tables[0].Rows.Count -1)
{
RecordCounter++;//before RecordCounter= 0, after RecordCounter=1
TxtDisplayQuestion.Text = dataset.Tables[0].Rows[ RecordCounter]["Emp_Title"].ToString(); //<= here: RecordCounter = 1
}
您可以将RecordCounter = -1设置为First或移动代码RecordCounter ++;设置TxtDisplayQuestion.Text = ...
You can set RecordCounter = -1 as First or move code RecordCounter++; after set TxtDisplayQuestion.Text=...
void NextRecord()
{
if ( RecordCounter < dataset.Tables[0].Rows.Count -1)
{
TxtDisplayQuestion.Text = dataset.Tables[0].Rows[ RecordCounter]["Emp_Title"].ToString();
RecordCounter++;
}
这篇关于为什么我的datagridview没有选择第一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!