C# 如何从数据库绑定 GridView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18960601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Bind a GridView from database
提问by Mrityunjay Malliya
How to bind a GridView?
如何绑定GridView?
I want to display my table data in a gridview.
我想在 gridview 中显示我的表格数据。
I have createed SQL table EmpDetail
with columns ID, Name, Salary Data
我已经创建了EmpDetail
带有列的SQL 表ID, Name, Salary Data
回答by Soner Sevinc
Try below code according to your scenario
根据您的情况尝试以下代码
I hope it helps you
我希望它能帮助你
protected void GridviewBind ()
{
using (SqlConnection con = new SqlConnection("Data Source=RapidProgramming;Integrated Security=true;Initial Catalog=RPDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select Name,Salary FROM YOUR TABLE", con);
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
con.Close();
}
}
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#3366CC" BorderStyle="None"
BorderWidth="1px" CellPadding="4"
style="text-align: center; margin-left: 409px" Width="350px">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>;
回答by Humpy
You could simply use the SqlDataSource. You would move the SqlDataSource from the toolbox where it says Data, SqlDataSource. You would then configure the datasource using the smart tag. Then using the smart tag on the gridview, select the SqlDataSource you placed onto the aspx page. This is really quick and requires little to no coding. http://msdn.microsoft.com/En-us/Library/z72eefad.aspxthis will show you a little bit more. Hope this helps you!
您可以简单地使用 SqlDataSource。您将从工具箱中移动 SqlDataSource,它显示数据、SqlDataSource。然后,您将使用智能标记配置数据源。然后使用 gridview 上的智能标记,选择您放置在 aspx 页面上的 SqlDataSource。这真的很快,几乎不需要编码。http://msdn.microsoft.com/En-us/Library/z72eefad.aspx这将向您展示更多。希望这对你有帮助!
回答by Ali
In order to run this code, you need to replace connectionstring's credentials myServerName\myInstanceName, myDataBase, myUsername, myPassword with yours
为了运行此代码,您需要将连接字符串的凭据 myServerName\myInstanceName、myDataBase、myUsername、myPassword 替换为您的凭据
using System.Data;
using System.Data.SqlClient;
string sConnectionString = @"Data Source=myServerName\myInstanceName;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;";
protected void Page_Load(object sender, EventArgs e){
if(!IsPostBack)
BindGridView();
}
private void BindGridView() {
DataTable dt = new DataTable();
SqlConnection con = null;
try {
string sQuery = "SELECT ID, Name, Salary FROM EmpDetail";
SqlConnection con = new SqlConnection(sConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand(sQuery, con);
SqlDataReader sdr = cmd.ExecuteReader();
dt.Load(sdr);
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
catch{ }
finally{
dt.Dispose();
con.Close();
}
}
回答by Meena
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
bindData();
}
}
public void bindData() {
SqlConnection con=new SqlCponnection(ConnectionStrings);
SqlDataAdapter da = new SqlDataAdapter("select * from Your TableName", con);
DataSet ds = new DataSet();
try {
da.Fill(ds, "YourTableName");
GridView1.DataSource = ds;
GridView1.DataBind();
} catch (Exception e) {
Response.Write( e.Message);
} finally {
ds.Dispose();
da.Dispose();
con.Dispose();
}
回答by Mohit Sharma
use Class7917
select * from Emp
alter table Emp add images varchar(100)
sp_helptext 'usp_emp_insert_update'
alter proc usp_emp_insert_update
@empid int,
@name varchar(50),
@cid int,
@sid int,
@dob datetime,
@isactive int,
@hobbies varchar(100),
@images varchar(100)
as
begin
if(@empid=0)
begin
insert into Emp(Name,cid,sid,dob,isactive,hobbies,images)
values(@Name,@cid,@sid,@dob,@isactive,@hobbies,@images)
end
else
begin
update Emp set Name=@name,cid=@cid,sid=@sid,
dob=@dob,isactive=@isactive,hobbies=@hobbies,images=@images
where EmpID=@empid
end
end
truncate table Emp
回答by bilal chaudhari
try this....
尝试这个....
protected void Page_Load(object sender, EventArgs e)
{
using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("select * from Table1", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
conn.Close();
}
}
<div>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>