Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Check the null values before fetching data to gridview

gridview null grid

  • Please log in to reply
3 replies to this topic

#1 qqaadir

qqaadir

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 12 March 2012 - 01:21 AM

Hi,

I have Gridview in which I have Imported data from the exccel file.Now I want that All records in the grid shouldn't be displayed.I have use the following code but not getting the desired results.
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;
        }
    }
}

  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 12 March 2012 - 07:36 AM

Without seeing the data, or the desired results, it's hard to advise you. I find it odd that you're checking for a space, but talk about checking nulls. It seems like you should add a WHERE clause to your SQL statement to handle the filtering.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 qqaadir

qqaadir

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 13 March 2012 - 01:32 AM

Yes I figured it out later that there is space not null in the blank column
Yes You are I have to handle it through query using where clause and that works OK
But In fact I need to do it through this event.Could You do some more to me about the code I have posted.
You have asked for the data,so it is not complicated case that you can not imagine it is the data from excel file
looks like
col1 col2 col2
val1 val2 val3
val4 val6
val7 val7
So I need that empty Field should not be displayed.Thank You Hope you got it.
  • 0

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 13 March 2012 - 05:11 AM

An empty field could be interpreted as '' or as null.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/






Also tagged with one or more of these keywords: gridview, null, grid

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download