Home > ASP.Net > Bind a to a GridView

Bind a to a GridView


Hi

try this example:

<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>" >
<head runat="server">
    <title>GridView Tips and Tricks Part 2</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID"
            DataSourceID="SqlDataSource1" ShowFooter="true" AllowPaging="True" AllowSorting="True"
            PageSize="5" OnRowDataBound="GridView1_RowDataBound">
            <Columns>                       
                <asp:TemplateField HeaderText="CategoryID" InsertVisible="False" SortExpression="CategoryID">
                    <ItemTemplate>
                        <asp:Label ID="lblCategoryID" runat="server" Text='<%# Bind("CategoryID") %>'></asp:Label>
                    </ItemTemplate>                  
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CategoryName" SortExpression="CategoryName">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtCategoryName" runat="server" Text='<%# Bind("CategoryName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblCategoryName" runat="server" Text='<%# Bind("CategoryName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Description" SortExpression="Description">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDesc" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblDesc" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
                    </ItemTemplate>                               
                </asp:TemplateField>               
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=SUPROTIM;Initial Catalog=Northwind;Integrated Security=True"
            ProviderName="System.Data.SqlClient" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]"
            UpdateCommand="UPDATE [Categories] SET [CategoryName] = @CategoryName, [Description] = @Description WHERE [CategoryID] = @CategoryID"/>
    </div>
    </form>
</body>
</html>

The web.config holding the connection will look similar to the following:

<configuration>
      <appSettings/>
      <connectionStrings>
            <add name="NorthwindConnectionString" connectionString="Data Source =(local);Integrated Security = SSPI; Initial Catalog=Northwind;"/>
      </connectionStrings>
...
</configuration>

Create a class called ‘Employees’

public class Employee
{
    private string enm;
    private int ageofemp;
    private string department;
 
    public string EName
    {
        get
        {
            return enm;
        }
        set
        {
            enm = value;
        }
    }
 
    public int Age
    {
        get
        {
            return ageofemp;
        }
        set
        {
            ageofemp = value;
        }
    }
 
    public string Dept
    {
        get
        {
            return department;
        }
        set
        {
            department = value;
        }
    }
 
    public Employee(string ename, int age, string dept)
    {
        this.enm = ename;
        this.ageofemp = age;
        this.department = dept;
    }
 
 
}

in Code behind:

protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<Employee> emp = new System.Collections.Generic.List<Employee>();
        emp.Add(new Employee("Jack", 22, "Marketing"));
        emp.Add(new Employee("Anna", 28, "Advertising"));
        emp.Add(new Employee("Linra", 23, "Advertising"));
        emp.Add(new Employee("Jacob", 44, "Production"));
        emp.Add(new Employee("Zinger", 28, "PPC"));
 
        GridView1.DataSource = emp;
        GridView1.DataBind();
 
 
    }

Good Luck

Advertisements
Categories: ASP.Net Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: