从datagridview中选择随机记录 [英] Selecting random record from datagridview

查看:84
本文介绍了从datagridview中选择随机记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的Windows窗体项目中有一个数据网格视图,里面装满了数据..我还有一个按钮(nextButton),单击以浏览这些记录。有没有办法,只要点击下一个按钮,我就可以从数据网格视图中选择一个随机记录。



这是我填充数据网格视图的代码。



Hi,

I have got a datagrid view in my windows form project that is is filled up with data.. I have also a button (nextButton) that is clicked to go through these records. Is there a way I can select a random record from the datagrid view whenever the next button is clicked.

This is my code that fills up the 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"];





以下是通过记录的nextButoon的代码





And here is the code for the nextButoon that goes through records

void NextRecord()
       {
           if ( RecordCounter < dataset.Tables[0].Rows.Count -1)
           {
               RecordCounter++;
               TxtDisplayQuestion.Text = dataset.Tables[0].Rows[ RecordCounter]["Emp_Title"].ToString();
   

           }





谢谢



Thank you

推荐答案

试试这个:

Try this:
Random rand = new Random();
int rowCount = dataset.Tables[0].Rows.Count;
int randomRow = rand.Next(rowCount); // get random row from 0 to rowCount - 1 inclusive


这篇关于从datagridview中选择随机记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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