ASP.NET - How to give space in two row of gridview from code behind

Asked By Sam Sam on 13-Oct-11 09:32 AM

I want to give space between two rows of girdview.
like this 

abc
abc

abc 
anc

dfg
dgc

Its not working 

if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridViewRow row = e.Row;
            HyperLink lblProfileId = (HyperLink)e.Row.FindControl("imgPhoto");

            if (lblProfileId.Text == "Break")
            {
                e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace( Environment.NewLine, "<br/>");
            }




        }


also try
 to add new row in datatable  before binding to girdview. Both are not working.
smr replied to Sam Sam on 13-Oct-11 09:36 AM
hi

You can use CellPadding &/or CellSpacing (properties of Table).  Otherwise clarify where you want to add the spaces.
smr replied to Sam Sam on 13-Oct-11 09:37 AM
you can try this

you can use  table cellspacion property

 

Table bt = new Table();

tb.CellSpacing = 2;

http://msdn.microsoft.com/es-es/library/system.web.ui.webcontrols.table.cellspacing%28VS.80%29.aspx

Reena Jain replied to Sam Sam on 13-Oct-11 09:42 AM
Hi,

Try to set CellPadding="5" and/or CellSpacing="5" or change the numbers for them along with setting BorderWidth and BorderColor.

There is another way to add some space between GridView's row through code:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
 
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
 
       TableCell tc = new TableCell();
       tc.Text = " ";
       tc.Height = Unit.Pixel(10);//you can change this to adjust to space you want
       
       GridView gv1=(GridView)sender; 
       //tc.Attributes["ColSpan"] = gv1.Columns.Count.ToString();
 
       tc.ColumnSpan= gv1.Columns.Count;
 
        
 
       GridViewRow gr = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
       gr.Cells.Add(tc);
 
       Table gvTable = (Table)e.Row.Parent;
       gvTable.Controls.Add(gr);
 
     }
 
   }
Sam Sam replied to smr on 13-Oct-11 10:13 AM
I want to add space when my condition match

ie. anc!=dfg  Then i want to add space to gridview rows


abc 
anc

dfg
dgc
dipa ahuja replied to Sam Sam on 13-Oct-11 10:19 AM
Just change the text of lblProfileId:

if (e.Row.RowType == DataControlRowType.DataRow)
{
  GridViewRow row = e.Row;
  HyperLink lblProfileId = (HyperLink)e.Row.FindControl("imgPhoto");
  if (lblProfileId.Text == "Break")
  {
    Label lblBreak = new Label();
    lblBreak.Text = "<br>";
        
    e.Row.Cells[0].Controls.Add(lblBreak);
  }
}