如何在c#.net windows应用程序的datagridview中使用sqldatareader显示多行? [英] How to display multiple rows using sqldatareader in datagridview in c# .net windows application?

查看:94
本文介绍了如何在c#.net windows应用程序的datagridview中使用sqldatareader显示多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我使用此代码在 DataGridView 中使用 SqlDataReader ,但只显示一行。

此表中有多行,但只显示一行。

  private   void  CreateColumns()
{
var obj = new ConnectionClass();
SqlCommand cmd = new SqlCommand( select FloorTileID,FloorTatName,FloorTileName,FloorTexture1,FloorTexture2,FloorTexture3,FloorTexture4,FloorTexture5,FloorTexture6,FloorTile的FloorTilePath,obj.GetConnection());

SqlDataReader sdr = cmd.ExecuteReader();

for int i = 0 ; i < dgvDisplayTiles.Rows.Count; i ++)
{
if (sdr.Read())
{
dgvDisplayTiles.Rows [i] .Cells [ 0 ]。Value = sdr [ 0 ]。ToString();
dgvDisplayTiles.Rows [i] .Cells [ 1 ]。值= sdr [ 1 ]的ToString();
dgvDisplayTiles.Rows [i] .Cells [ 2 ]。值= sdr [ 2 ]的ToString();
Bitmap img;
img =(Bitmap)Image.FromFile(Application.StartupPath + sdr [ 3 ]。ToString());
dgvDisplayTiles.Rows [i] .Cells [ 3 ]。Value = img;
}
}

sdr.Close();
}



请帮帮我。



先谢谢。



Ankit Agarwal

软件工程师

解决方案

解决方案



你使用 if(sdr.Read()),所以它会满足一次。



您需要使用 while(sdr.Read()),它将满足Reader读取的所有行。

Hello,

I am using this code for display rows in DataGridView using SqlDataReader, but only one row displayed.
Multiple rows in this table, but only one row displayed.

private void CreateColumns()
{   
    var obj = new ConnectionClass();
    SqlCommand cmd = new SqlCommand("select FloorTileID,FloorCatName,FloorTileName,FloorTexture1,FloorTexture2,FloorTexture3,FloorTexture4,FloorTexture5,FloorTexture6,FloorTilePath from FloorTile", obj.GetConnection());

    SqlDataReader sdr = cmd.ExecuteReader();

    for (int i = 0; i < dgvDisplayTiles.Rows.Count; i++)
    {
        if (sdr.Read())
        {
            dgvDisplayTiles.Rows[i].Cells[0].Value = sdr[0].ToString();
            dgvDisplayTiles.Rows[i].Cells[1].Value = sdr[1].ToString();
            dgvDisplayTiles.Rows[i].Cells[2].Value = sdr[2].ToString();
            Bitmap img;
            img = (Bitmap)Image.FromFile(Application.StartupPath + sdr[3].ToString());
            dgvDisplayTiles.Rows[i].Cells[3].Value = img;
        }
    }
    
    sdr.Close();
}


please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer

解决方案

Solution

You have used if (sdr.Read()), so it will be satisfied for once.

You need to use while (sdr.Read()), it will satisfy for all the rows read by Reader.


这篇关于如何在c#.net windows应用程序的datagridview中使用sqldatareader显示多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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