Ⅰ c#中代码绑定GridView数据源
改成码行如羡模册下兄宏代码:
protected void Button1_Click1(object sender, EventArgs e)
{
if (CheckBox1.Checked)
{
GridView1.DataSourceID = SqlDataSource1.ID;
}
else
{
GridView1.DataSourceID = SqlDataSource2.ID;
}
}
Ⅱ 如何以代码方式创建gridview,并将其绑定数据(C#)
这是个简易的代码演示怎么使用,一些其他的属性设置请自己查阅MSDNDataGridView gview = new DataGridView(); //实例化控件,最好是全专局属
gview.Dock = DockStyle.Fill;
this.Controls.Add(gview); //添加GRIDVIEW控件到指定的窗体,这里是直接添加到窗体
gview.DataSource = 数据源 //关键设置数据库,数据源就可以了
Ⅲ vs2010网站gridview怎么绑定数据源
前台: <asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="5"
AutoGenerateColumns="False" HeaderStyle-VerticalAlign="Middle" CellPadding="3"
Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
Height="221px" Width="500px" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" >
<Columns>
<asp:BoundField DataField="CID" HeaderText="用户ID" ReadOnly="true">
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="用户耐猛姓名" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Sex" HeaderText="性别" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Address" HeaderText="家庭住址" >
<ItemStyle Width="140px" />
</asp:BoundField>首虚
<asp:BoundField DataField="Post" HeaderText="邮政编码" >
<ItemStyle Width="50px" />
</asp:BoundField>
<者亩燃asp:CommandField HeaderText="编辑" ShowEditButton="True">
<ItemStyle Width="70px" />
</asp:CommandField>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<PagerSettings Visible="False" />
<FooterStyle Font-Bold="True" />
<HeaderStyle Font-Bold="False" Font-Italic="False" />
</asp:GridView>
后台:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Update : System.Web.UI.Page
{
SqlConnection sqlcon;
string strCon = "Data Source=(local);Database=wxd;Uid=sa;Pwd=sa";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
//绑定GridView
public void bind()
{
string sqlstr = "select * from Admin";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "Admin");
GridView1.DataSource = myds;
GridView1.DataBind();
}
//编辑行
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
bind();
}
//更新行
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string strsql = "update Admin set Name =@Name ,Sex =@Sex, Address =@Address,Post =@Post where CID = @CID ";
SqlCommand sqlcmd = new SqlCommand(strsql, sqlcon);
try
{
sqlcmd.Parameters.Add(new SqlParameter("@CID", SqlDbType.Int, 4));
sqlcmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar,50 ));
sqlcmd.Parameters.Add(new SqlParameter("@Sex", SqlDbType.VarChar, 50));
sqlcmd.Parameters.Add(new SqlParameter("@Address", SqlDbType.VarChar, 50));
sqlcmd.Parameters.Add(new SqlParameter("@Post", SqlDbType.VarChar, 50));
sqlcmd.Parameters["@CID"].Value = GridView1.Rows[e.RowIndex].Cells[0].Text;
sqlcmd.Parameters["@Name"].Value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
sqlcmd.Parameters["@Sex"].Value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
sqlcmd.Parameters["@Address"].Value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
sqlcmd.Parameters["@Post"].Value = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
sqlcmd.Connection.Open();
sqlcmd.ExecuteNonQuery();
this.GridView1.EditIndex = -1;
}
catch (SqlException ex)
{
throw ex;
}
sqlcmd.Connection.Close();
bind();
}
//取消编辑行
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
bind();
}
//设定列宽
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate) || e.Row.RowState == DataControlRowState.Edit)
{
for (int i = 1; i < GridView1.Columns.Count-1;i++ )
{
TextBox txt = (TextBox)e.Row.Cells[i].Controls[0];
txt.Width = Unit.Pixel(60);
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C9D3E2',this.style.fontWeight='';");
//当鼠标离开的时候 将背景颜色还原的以前的颜色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
e.Row.Attributes["style"] = "Cursor:pointer";
}
}
Ⅳ 关于asp.net的gridview控件的数据绑定问题,显示不出数据,求修改后的代码
建议使用后台历纳代码液烂源绑定gridview数据源闹态;
gridview.datasource=dataset.tables[0].defaultview;
gridview.pageindex=pageindex;
gridview.databind();
Ⅳ Asp.net中,Gridview控件想在后台进行数据绑定,应该怎么写代码(C#)
string
sql
=
"server=.;database=你的数据仔谈库念塌碰;integrated
security=sspi;";
string
s
=
"衫御select
SoftwareID,SoftwareName
from
Software";
SqlConnection
conn
=
new
SqlConnection(sql);
SqlDataAdapter
sda
=
new
SqlDataAdapter(s,conn);
DataSet
ds
=
new
DataSet();
sda.Fill(ds);
GridView1.DataSource
=
ds;
GridView1.DataBind();
Ⅵ C#窗体程序 GRIDVIEW数据绑定
gridview 通过引导绑定到所需的数据源宽首表,如:PersonBindsource;
然后在后台代码中:
public Formtest()
{
InitializeComponent();
personInfoBindingSource.DataSource = ds; //加入的代码,碧告为数据源绑定所需数据
personInfoBindingSource.ResetBindings(false); //更悔巧明新数据源绑定控件的数据显示 }
Ⅶ GridView1代码绑定数据源,无法获取列信息(c# 2008)(再次强调代码绑定)
<asp:BoundField HeaderText="公告标题" DataField="Notice_Titlechr" >
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Notice_Publisheddt"宴伍 HeaderText="发布时间">
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Notice_Authorchr" HeaderText="发布孝帆人">
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
上面的DataField="你要绑字的字段"
但是你巧祥雹的代码我就没看明白,为什么页面上你不放DataField?难道是和你下面的代码有关?
SqlDataSource1.SelectCommand = "select FID ,FName from UpFile";
GridView1.AutoGenerateColumns = true;
GridView1.DataSourceID = "SqlDataSource1";
Response.Write(GridView1.Columns.Count);
Response.Write(GridView1.Columns.Count);
通常做法就是this.GridView.DataSource = "集合";
this.GridView.DataBind();
然后页面里就像我给你发的最上面开头的那些代码。
PS:实现这些之前请保证你与数据库是连接在的,也就是说你的SQL语句是有效的。
Ⅷ 手动绑定GridView数据源
private void BindData()
{
string ConStr="data source=xhjj;user=gl;password=gl;";
OracleConnection MyCon = new OracleConnection(ConStr);
string QueryStr = "SELECT customerid,CompanyName,ContactName,Address FROM customers";
OracleDataAdapter Da = new OracleDataAdapter(QueryStr, MyCon);
DataSet Ds = new DataSet();
Da.Fill(Ds, "Customers");
GridView1.DataSource = Ds.Tables["陆衡Customers"].DefaultView; //修戚基改这高悉谨句
GridView1.DataKeyNames = new string[] { "customerid" };
GridView1.DataBind();
}
Ⅸ c# gridview绑定list数据源
在gridview添加列的地方,此宏让设置属性DataPropertyName的值为森局绝唯List的字段名
比如学生姓名,List<Student>,Student里学生姓名属性为StuName
那么就把上述的列的DataPropertyName属性赋成“StuName”
然后在gridview.DataSource=list的时候的,就显示出来了姓名了
要显示分数,同理