Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do i remove this blank space from gridview in asp.net?

please see this IMAGE

I want to remove this blank white space NOT hide this(as I know we can hide this border very easily)

even after hiding the border the white unused space is there
i want to remove that

please see this IMAGE

my gridview code is :-

ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="OrderID" OnRowDataBound="FindGridAndBound" GridLines="Horizontal">
				<AlternatingRowStyle BackColor="#F7F7F7" />
				<Columns>
					<asp:TemplateField>
						<ItemTemplate>
							<asp:GridView ID="GridViewNested" runat="server" AutoGenerateColumns="true" EnableViewState="false" 
								Width="100%" BackColor="Aquamarine" />
						</ItemTemplate>
					</asp:TemplateField>
				</Columns>
			</asp:GridView>


code behind :- [i have taken this code from this Tutorial VIDEO]

C#
using NoshCart.DataLayer;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

namespace NoshCart.Account
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetData();
            }
        }

        private void GetData()
        {
            //creating the sqlparameter object
            SqlParameter[] parameters = new SqlParameter[1];

            //adding the parameters
            parameters[0] = DataAccess.AddParameter("@custid", Convert.ToInt64(Session["Id"]), SqlDbType.BigInt, 20);

            //executing the Stored Procedure
            DataTable table = DataAccess.ExecuteDTByProcedure("SP_GetOrderIDNumbers", parameters);

            if (table.Rows.Count > 0)
            {
                GridView1.Visible = true;
                GridView1.DataSource = table;
                GridView1.DataBind();

            }
            else
            {
                LabelDataNotFound.Visible = true;
                LabelDataNotFound.Text = "No Orders Found";
            }
        }

        protected void FindGridAndBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow) return;

            GridView nestedGridView = (GridView)e.Row.FindControl("GridViewNested");
            
            int ordid = int.Parse(GridView1.DataKeys[e.Row.RowIndex].Value.ToString());

            //creating the sqlparameter object
            SqlParameter[] parameters = new SqlParameter[2];

            //adding the parameters
            parameters[0] = DataAccess.AddParameter("@custid", Convert.ToInt64(Session["Id"]), SqlDbType.BigInt, 20);
            parameters[1] = DataAccess.AddParameter("@orderid", Convert.ToInt64(ordid), SqlDbType.BigInt, 20);

            //executing the Stored Procedure
            DataTable table = DataAccess.ExecuteDTByProcedure("SP_GetAllOrderDetails", parameters);

            nestedGridView.DataSource = table;
            nestedGridView.DataBind();
        }
    }
}


AddParameter method :-

C#
public static SqlParameter AddParameter(string parameterName, object value, SqlDbType DbType, int size)
        {
            SqlParameter param = new SqlParameter();
            param.ParameterName = parameterName;
            param.Value = value.ToString();
            param.SqlDbType = DbType;
            param.Size = size;
            param.Direction = ParameterDirection.Input;
            return param;
        }


ExecuteDTByProcedure method :-

C#
public static DataTable ExecuteDTByProcedure(string ProcedureName,SqlParameter[] Params)
  {
      SqlConnection conn = new SqlConnection(ConnectionString);
      SqlCommand cmd = new SqlCommand();
      cmd.Connection = conn;
      cmd.CommandText = ProcedureName;
      cmd.Parameters.AddRange(Params);
      cmd.CommandType = CommandType.StoredProcedure;
      SqlDataAdapter adopter = new SqlDataAdapter(cmd);
      DataTable dTable = new DataTable();

      try
      {
          adopter.Fill(dTable);
      }

      catch (Exception ex)
      {
          string str = ex.Message;
      }

      finally
      {

          adopter.Dispose();
          cmd.Parameters.Clear();
          cmd.Dispose();
          conn.Dispose();
      }
      return dTable;
  }


please help

thanks :)

What I have tried:

i have tried many thing but NONE of these are working at all

C#
e.Row.Cells[0].Visible = false;


C#
gridView1.Columns[0].Visible = false;


HTML
RowHeadersVisible = "false"


HTML
ColumnHeadersVisible = "false"
Posted
Updated 9-Jan-22 23:44pm
v2
Comments
Karthik_Mahalingam 18-Nov-17 21:11pm    
nested gridview?
show the codebehind
Palash Sachan 18-Nov-17 22:23pm    
yes sir its the nested gridview..i have updated the question..added the code behind and other methods also..please check and please help if u know the answer..thanks
Karthik_Mahalingam 19-Nov-17 11:33am    
use  Reply   button to post comments/query to the concerned user, so that the user gets notified and respond to your text.
Palash Sachan 20-Nov-17 12:25pm    
sorry i forgot..
Karthik_Mahalingam 20-Nov-17 22:24pm    
fine, take care in further posts

try
protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               GetData();
               GridView1.HeaderRow.Visible = false;
           }
       }
 
Share this answer
 
Comments
Palash Sachan 20-Nov-17 12:24pm    
working perfectly..thank u sir :)
Karthik_Mahalingam 20-Nov-17 22:24pm    
welcome
Try this. Deleting or editing before binding to datatable is easier and more secure I think.

public static void RemoveEmptyDataTable(DataTable dt)
        {
            for (int i = dt.Rows.Count - 1; i >= 0; i--)
            {
                if (dt.Rows[i][0].ToString() == "" || dt.Rows[i][1].ToString() == "any_string")
                    dt.Rows[i].Delete();
            }
            dt.AcceptChanges();
        }


and call this like this:

oleDbDataAdapter.Fill(dataTable);
            RemoveEmptyDataTable(dataTable);

            GvDetay.DataSource = dataTable;
 
Share this answer
 
Comments
CHill60 10-Jan-22 7:22am    
This is deleting empty rows from the datatable. The OP wanted the empty space in the headers removing - see their comment to Solution 1

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