使用C#验证GridView列 [英] Validating GridView Column using C#

查看:75
本文介绍了使用C#验证GridView列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的asp.net Page 上有GridView ,我想验证Gridview.
我希望在Grid中不显示具有Null Value的记录,就像我在第二列行中有一些Null值一样.所以我需要隐藏那些Null值.实际上,不应显示具有单个null 值的整个row,并且我已从excel将数据导入网格中.
我已使用此代码
这是我用来导入excel文件的代码.

Hi,
I have GridView on my asp.net Page I want to validate the Gridview.
I want that in my Grid , Records with Null Value do not be displayed, like I have some Null values in second column rows. So I need to hide those Null values. In fact entire row with a single null value shouldn''t be displayed and I have imported the data in grid from excel.
I have Used this code
This the code that I have use to import the excel file.

protected void Button1_Click(object sender, EventArgs e)
        {
            string target = Server.MapPath("~/Upload");
            if (FileUpload1.HasFile)
            {
                FileUpload1.SaveAs(System.IO.Path.Combine(target, FileUpload1.FileName));

                string connectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", target + "\\" + FileUpload1.FileName);
                string query = String.Format("select * from [{0}$]", "Sheet1");
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
                DataSet dataSet = new DataSet();

                dataAdapter.Fill(dataSet);
                GridView1.DataSource = dataSet.Tables[0];
                GridView1.DataBind();

            }
        }





protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string dat = DataBinder.Eval(e.Row.DataItem, "a10").ToString();
        if (dat.Equals(" "))
        {
            // if you use ImageField to display image 
            DataRowView rowView = (DataRowView)e.Row.DataItem;
            rowView.Row["a10"].ToString();
  
            // if you use Image control to display image 
            GridView image = (GridView)e.Row.FindControl("GridView1");

            GridView1.Columns[0].Visible = true;
            image.Visible = false;
        }
    }
}

推荐答案

", ); OleDbDataAdapter dataAdapter = OleDbDataAdapter(query,connectionString); DataSet dataSet = DataSet(); dataAdapter.Fill(dataSet); GridView1.DataSource = dataSet.Tables [ 0 ]; GridView1.DataBind(); } }
", "Sheet1"); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString); DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet); GridView1.DataSource = dataSet.Tables[0]; GridView1.DataBind(); } }





protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string dat = DataBinder.Eval(e.Row.DataItem, "a10").ToString();
        if (dat.Equals(" "))
        {
            // if you use ImageField to display image 
            DataRowView rowView = (DataRowView)e.Row.DataItem;
            rowView.Row["a10"].ToString();
  
            // if you use Image control to display image 
            GridView image = (GridView)e.Row.FindControl("GridView1");

            GridView1.Columns[0].Visible = true;
            image.Visible = false;
        }
    }
}


将记录提取到gridview之前,请通过查询检查是否为空值.

仅将那些没有空值的记录插入网格中.

这样,您就不会在gridview中具有空值的记录.

希望您对此有所了解.
Before fetching the records into gridview, check for the null values through the query.

Insert only those records in the grid which does not have null values.

In this way you will not have the records with null values in gridview.

Hope you got an idea through this.


foreach (DataRow drTemp in ds.Tables[0].Select("a10 IS NOT NULL")
{
// Fetch the records in new dataset which is not null
}


这篇关于使用C#验证GridView列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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