Sep 22 2009

Gridview Template Field

Step1: Drag Gridview control from Toolbox Data Controls into Design

Page (Default.aspx)

Step2: Keep AutoGenereateColumns Property false in Gridview Properties

gridview edit template

gridview edit template

Step 3:First click -> Edit columns.. -> choose TemplateField –> Add –> ok

adding_gridview_templatefield

gridview_edit_template

gridview_edit_template

Step 4: see the above figure now click Edit Template

(Display Mode must be in Item Template)

gridview_ItemTemplate

gridview_ItemTemplate

Step5: Adding controls and table inside item template to display data

add_controls_grdiview_ItemTemplate

add_controls_grdiview_ItemTemplate

3 label controls and one Textbox with Multiline property True

Step6: Binding data into Item template field label control

Choose label control (see above figure) à click Edit DataBindings

gridview_DataBinding

gridview_DataBinding

Similary for other two label controls and textbox bind values

LastName = DataBinder.Eval(Container.DataItem,”LastName”)

FirstName = DataBinder.Eval(Container.DataItem,”FirstName”)

Title = DataBinder.Eval(Container.DataItem,”Title”)

Notes = DataBinder.Eval(Container.DataItem,”Notes”)

Instead of DataBinder.Eval(Container.DataITem,”Notes”)

or

Eval(“Notes”)

Desing code in Default.aspx page

<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False”

ShowHeader=”False”>

<Columns>

<asp:TemplateField>

<ItemTemplate>

<table cellpadding=”2″ cellspacing=”2″ class=” ”

style=”font-family: Verdana; font-size: 9pt; border: 1px solid #999999″

width=”500px”>

<tr>

<td>

<table cellpadding=” ” cellspacing=” ” width=”500px”>

<tr>

<td>

&nbsp;</td>

<td>

&nbsp;</td>

</tr>

<tr>

<td>

LastName</td>

<td>

<asp:Label ID=”Label1″ runat=”server”

Text=’<%# DataBinder.Eval(Container.DataItem,”LastName”) %>’></asp:Label>

</td>

</tr>

<tr>

<td>

FirstName</td>

<td>

<asp:Label ID=”Label2″ runat=”server”

Text=’<%# DataBinder.Eval(Container.DataItem,”FirstName”) %>’ > /asp:Label>

</td>

</tr>

<tr>

<td>

Title</td>

<td>

<asp:Label ID=”Label3″ runat=”server”

Text=’<%# DataBinder.Eval(Container.DataItem,”Title”) %>’>

</asp:Label>

</td>

</tr>

<tr>

<td>

Notes</td>

<td>

<asp:TextBox ID=”TextBox1″ runat=”server”

Text=’<%# DataBinder.Eval(Container.DataItem,”Notes”) %>’ TextMode=”MultiLine”                                                   Width=”204px”></asp:TextBox>

</td>

</tr>

</table>

</td>

</tr>

</table>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

Write Below code in Default.aspx.cs

using System.Data;

using System.Data.SqlClient;

protected void Page_Load(object sender, EventArgs e)

{

GetEmployees();

}

public void GetEmployees()

{

string ConnectionString = “Data Source=192.168.1.23;Initial Catalog=NorthWind;User Id=sa;Password=abc;”;

SqlConnection objConn = new SqlConnection(ConnectionString);

string query = “select FirstName,LastName,Title,Notes from Employees“;

SqlCommand objCmd = new SqlCommand(query, objConn);

objCmd.CommandType = CommandType.Text;

DataSet objDs = new DataSet();

SqlDataAdapter objDa = new SqlDataAdapter(objCmd);

objConn.Open();

objDa.Fill(objDs);

objCmd.ExecuteNonQuery();

GridView1.DataSource = objDs;

GridView1.DataBind();

objConn.Close();

}

Now Run the Application

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Leave a Reply

Alibi3col theme by Themocracy