如果有数据,如何填充datagridview [英] How to populate datagridview only if there is data

查看:83
本文介绍了如果有数据,如何填充datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个带有两个文本框,一个组合框,一个按钮和一个datagridview的窗体。使用存储过程绑定datgridview,即填充文本框和组合框并单击按钮必须填充datagridview。我只有在有数据时才需要填充datagridview。如果数据为空,datagidview应显示找不到记录。



< b>我尝试了什么:



Hi all,
I have a windows form with two textbox,one combobox,one button and a datagridview .The datgridview is binded using stored procedure i.e when the textbox and combobox is filled and click button the datagridview must be populated.I need to populate datagridview only if there is a data.If the data is empty the datagidview should display "No records found".

What I have tried:

private void button2_Click(object sender, EventArgs e)
       {

           if (txt_VoucherNo.Text.Trim() != "")
           {
               Voucher = txt_VoucherNo.Text.Trim();
           }
           if (ddl_prt.SelectedValue.ToString().Trim() != "")
           {
               prt = ddl_prtSelectedValue.ToString().Trim();
           }

           if (batch.Text.Trim() != "")
           {
               batch = txt_batchno.Text.Trim();
           }


           //GET Data From Database
           var connectionstring=ConfigurationManager.ConnectionStrings["PROCConnectionString"].ConnectionString;
           SqlConnection cn = new SqlConnection(connectionstring);


           SqlCommand cmd = new SqlCommand();
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.CommandText="SP44";

           cmd.Parameters.AddWithValue("Voucher", Voucher);
           cmd.Parameters.AddWithValue("prt", prt);
           cmd.Parameters.AddWithValue("batch", batch);
           cmd.CommandTimeout = 999999;
           cmd.Connection=cn;

           try
           {
               cn.Open();
               DataTable dt = new DataTable();
               //dt = cmd.ExecuteReader();
               SqlDataReader dr = cmd.ExecuteReader();
               dt.Load(dr);
               dataGridView1.DataSource = dt;



           }
           catch (Exception ex)
           {

           }
           finally
           {
               cmd.Connection.Close();
               cn.Close();
           }


       }





//这是我的单击按钮时的代码。如果没有数据,则加载datgridview(仅标题)。如果有数据,我怎样才能加载datagridview。非常感谢任何帮助。



//This is my code when the button is clicked.Here the datgridview (only headers) are loaded if there is no data.How can I load the datagridview only if there is data.Any help will be really appreciated.

推荐答案

尝试



try

DataTable dt = new DataTable();
         SqlDataAdapter da = new SqlDataAdapter(cmd);
         da.Fill(dt);
         if (dt.Rows.Count == 0) {
             DataTable dtNoRows = new DataTable();
             dtNoRows.Columns.Add("Info");
             dtNoRows.Rows.Add("No records found");
             dataGridView1.DataSource = dtNoRows;
         }
         else
             dataGridView1.DataSource = dt;


这篇关于如果有数据,如何填充datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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