0
check this:
in aspx page:
<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="StudentID">
<ItemTemplate>
<asp:Label ID="lblStudentId" Text='<% #Eval("StudentID") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name">
<ItemTemplate>
<asp:Label ID="lblStudentName" runat="server" Text='<% #Eval("StudentName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
in cs:
add the namespaces:
using System.Data.SqlClient;
using System.Data;
write this code in page load:
string connstr = "Data Source=WIN2K8; Initial Catalog=ABC;User ID=sa; Password=123";
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connstr);
string strsql = "Select StudentID,StudentName from student";
SqlDataAdapter da = new SqlDataAdapter(strsql, conn);
da.Fill(ds, "student");
grid.DataSource = ds.Tables["student"];
grid.DataBind();
Accepted 0
no not this databind in gridview like template option in ASP script
0
Step 1: Create new Project
Launch Visual Studio 2010, click on File->New->Website to create a new ASP.NET website Project.
Step 2: Create a new Page
Right click on the Project on the Solution Explorer Panel. Select Add->New Item. Then, choose Web Form. I'd like to name it Products.aspx
Step3: Add the following code to the Products.aspx file
<form id="form1" runat="server">
<div>
<asp:GridView ID="grid" runat="server">
</asp:GridView>
</div>
</form>
then go to .cs and
add the namespaces:
using System.Data.SqlClient;
using System.Data;
write this code in page load:
string connstr = "Data Source=WIN2K8; Initial Catalog=ABC;User ID=sa; Password=123";
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connstr);
string strsql = "Select * from Products";
SqlDataAdapter da = new SqlDataAdapter(strsql, conn);
da.Fill(ds, "ProductsTable");
grid.DataSource = ds.Tables["ProductsTable"];
grid.DataBind();
change the connection string as your and change the query and check.