how to bind data in griedviw in asp.net

Posted by Shanky11 under ASP.NET on 3/6/2013 | Points: 10 | Views : 879 | Status : [Member] | Replies : 4
this is the design of griedview whch i need to bind to database
how can i get ths ???

<asp:GridView ID="contactgrdview" runat ="server" AutoGenerateColumns="false" AllowPaging ="true">
<Columns>

<asp:BoundField DataField="username" HeaderText="Name" />
<asp:BoundField DataField ="Mobile" HeaderText ="Mobile-No" />

<asp:TemplateField HeaderText ="Select">

<ItemTemplate >
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>




Responses

Posted by: Pavanandey on: 3/6/2013 [Member] Bronze | Points: 25

Up
0
Down
string strCon = "Data Source=" ";Initial Catalog=" ";Integrated Security=True";
string strSQL = "select username,Mobile from <<table>>";

SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

DataTable table = new DataTable();
dataAdapter.Fill(table);
contactgrdview.DataSource = table;
contactgrdview.DataSource = dbBindSource;



Thanks
Pavan Kumar
Mark Answer if this fits the need

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 3/6/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication16.WebForm3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="contactgrdview" runat ="server" AutoGenerateColumns="false" AllowPaging ="true">
<Columns>

<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField ="Mobileno" HeaderText ="Mobile-No" />

<asp:TemplateField HeaderText ="Select">

<ItemTemplate >
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace WebApplication16
{
public partial class WebForm3 : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;

protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from emp";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "emp");
contactgrdview.DataMember = "emp";
contactgrdview.DataSource = ds;
contactgrdview.DataBind();
con.Close();
}
}
}


If this post helps you mark it as answer
Thanks

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 3/7/2013 [Member] Starter | Points: 25

Up
0
Down
if still dint come set AutoGenerateColumns="true"

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raj.Trivedi on: 3/7/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.IO;
using System.Data.SqlClient;


public partial class UploadandViewPDF : System.Web.UI.Page
{
string strconn = ConfigurationManager.ConnectionStrings["cnnLocal"].ConnectionString;
string desc = "0";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}




private void BindGrid()
{
try
{
SqlConnection con = new SqlConnection(strconn);
con.Open();
string str = "select * from emp";
SqlCommand cmdins = new SqlCommand(str);
cmdins.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("ShowGrid", con);
DataSet ds = new DataSet();
da.Fill(ds, "pdffiles");
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
desc = ex.Message;
}

}





}


Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response