Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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();

            }
        }


C#
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;
        }
    }
}
Posted
Updated 11-Mar-12 22:50pm
v4
Comments
M Ali Qadir 8-Mar-12 11:54am    
Is there no Ans Please Reply.
ratheesh.kumar88 23-Dec-13 0:01am    
Hi,

Please give me the code for gridview search coding using 3-tier application.
bala.vanchi 9-Mar-12 3:55am    
you can use ((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[10]==null then you can add coditions
ProEnggSoft 11-Mar-12 3:25am    
Edit: Indentation organized. code tags for keywords added - PES
M Ali Qadir 11-Mar-12 5:07am    
Hi,Bala this code means that I am checking nulls after that i want that these null value shouldn't be displayed so writing simply "GridView1.Columns[0].Visible = true;" would be enough or not?

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.
 
Share this answer
 
Comments
M Ali Qadir 11-Mar-12 4:18am    
Yes That's the great idea "Anik" but How Could I implement on which event of grid would you please give an example,thank you
Aniket Yadav 11-Mar-12 4:59am    
Post the sample code of how you enter the records in Gridview.
M Ali Qadir 12-Mar-12 4:47am    
Hi,Anik
In fact I have imported the records from excel file,So I am Improving question with more code that tell that excel file imported to grid.
Aniket Yadav 12-Mar-12 5:04am    
1. so you have taken the data from excel into dataset... right?
2. Now you can select the record from dataset where your second column is not null.
3. And fetch the output of this into new dataset.
4. Finally fire GridView1.DataSource = newlycreateddataSet.Tables[0];
M Ali Qadir 12-Mar-12 5:26am    
Yes from Excel to dataset,Sorry But I did not Understand Please elaborate more.
C#
foreach (DataRow drTemp in ds.Tables[0].Select("a10 IS NOT NULL")
{
// Fetch the records in new dataset which is not null
}
 
Share this answer
 
Comments
M Ali Qadir 12-Mar-12 9:43am    
there may be problem with syntax "foreach (DataRow drTemp in ds.Tables[0].Select("a10 IS NOT NULL")"
It Works as my senior suggests thank you anik
string query = String.Format("select * from [{0}$] where a10 <> ''", "Sheet1");
 
Share this answer
 
It works If dont use hard coded query
SO I have done it with this event also
C#
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRowView rowView = (DataRowView)e.Row.DataItem;
        //rowView.Row["a10"].ToString();

        if (((DataRowView)e.Row.DataItem)["a10"] ==null)
        {
            GridView1.Columns[1].Visible = false;
        }
        if (e.Row.Cells[1].Text == "&nbsp;")
        {

            e.Row.Cells[1].Visible = false;
            //GridView1.SelectedRow.Visible = false;
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900