Ⅰ 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的時候的,就顯示出來了姓名了
要顯示分數,同理