当从下拉列表中选择项目时,如果gridview中不存在记录,则显示未找到的记录 [英] Display record not found, if recordsnot exists in gridview, when item selected from dropdownlist

查看:63
本文介绍了当从下拉列表中选择项目时,如果gridview中不存在记录,则显示未找到的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果从下拉列表中选择项目时,如果gridview中不存在记录,则找不到显示记录

====================== ================================================== ==========================



嗨frnds,



正在使用asp.net c#,sqlserver2005,


在我的网页上我有一个下拉列表和一个gridview。 4个文本框,用于从数据库中读取数据,并在gridview& ;;文本框根据下拉列表选择项目,



根据下拉列表,如果gridview中不存在记录。显示未找到的消息记录。



这是我的代码。



  protected   void  DDLStudents_SelectedIndexChanged( object  sender ,EventArgs e)
{

SqlConnection conDisp = new SqlConnection(_connString);
SqlCommand cmdDisp = new SqlCommand();
cmdDisp.Connection = conDisp;

cmdDisp.CommandText = 从Student_Records中选择*,其中Student_code =' + DDLStudents.SelectedValue + ';
使用(conDisp)
{
conDisp.Open();
SqlDataReader readerDisp = cmdDisp.ExecuteReader();
if (readerDisp.Read())
{
TxtPrincipal.Text = readerDisp [ Principal]。ToString();
TxtTeacher.Text = readerDisp [ 教师]。ToString();
TxtEmail.Text = readerDisp [ Email]。ToString();
TxtTelephone.Text = readerDisp [ TelephoneNo]。ToString();

GridBind();


}


其他
{


TxtPrincipal.Text = ;
TxtTeacher.Text = ;
TxtEmail.Text = ;
TxtTelephone.Text = ;
}
}


}


public void GridBind()
{

SqlConnection con = new SqlConnection (_connString);
SqlCommand cmd = new SqlCommand( select StudentName,RollNo,FathersName,Student_Records的标准,其中Student_code =' + DDLStudents.SelectedValue + ' ,con);
SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
Adpt.Fill(dt);
lblCount.Text = dt.Rows.Count.ToString();
lblCount.Visible = true ;
GVView.DataSource = dt;
GVView.DataBind();

}







在gridview中找不到记录时显示消息下拉列表。



注意:Pageloads时不会显示Gridview。根据下拉列表选择项目gridview将显示。



请提前帮助谢谢。

解决方案

尝试这个,



使用GridView的EmptyDataText属性。



 <   asp:GridView     EmptyDataText   = 找不到记录。    ID   =  grddata    runat   =  server >  
< / asp:GridView >


Display record not found, if records not exists in gridview, when item selected from dropdownlist
==================================================================================================

Hi frnds,

am working on asp.net c#, sqlserver2005,

on my webpage i have a dropdownlist and a gridview. and 4 textboxes to read the data from database am displaying data in gridview & textboxes according to dropdownlist selected item,

according to dropdownlist if records not exists in gridview. Display a message records not found.

This is my code.

protected void DDLStudents_SelectedIndexChanged(object sender, EventArgs e)
    {

        SqlConnection conDisp = new SqlConnection(_connString);
        SqlCommand cmdDisp = new SqlCommand();
        cmdDisp.Connection = conDisp;
      
        cmdDisp.CommandText = "Select * from Student_Records where Student_code ='" + DDLStudents.SelectedValue +"'";
        using (conDisp)
        {
            conDisp.Open();
            SqlDataReader readerDisp = cmdDisp.ExecuteReader();
            if (readerDisp.Read())
            {
                TxtPrincipal.Text = readerDisp["Principal"].ToString();
                TxtTeacher.Text = readerDisp["Teacher"].ToString();
                TxtEmail.Text = readerDisp["Email"].ToString();
                TxtTelephone.Text = readerDisp["TelephoneNo"].ToString();

                GridBind();


            }
            

            else
            {


                TxtPrincipal.Text = "";
                TxtTeacher.Text = "";
                TxtEmail.Text = "";
                TxtTelephone.Text = "";
            }
        }
       
        
        }
  
  
        public void GridBind()
    {
        
        SqlConnection con = new SqlConnection(_connString);
        SqlCommand cmd = new SqlCommand("select StudentName,RollNo, FathersName, Standard from Student_Records where Student_code = '" + DDLStudents.SelectedValue + "'", con);
        SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        Adpt.Fill(dt);
        lblCount.Text = dt.Rows.Count.ToString();
        lblCount.Visible = true;
        GVView.DataSource = dt;
        GVView.DataBind();

    }




display a message when records not found in gridview according to dropdownlist.

Note: Gridview will not be displayed when Pageloads. Based on dropdownlist selected item gridview will be displayed.

Please help thanks in advance.

解决方案

Try this,

use EmptyDataText property of GridView.

<asp:GridView EmptyDataText="No Record Found." ID="grddata" runat="server">
    </asp:GridView>


这篇关于当从下拉列表中选择项目时,如果gridview中不存在记录,则显示未找到的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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