在GridView中如何插入要保存在数据库中的数据和数据 [英] In GridView how to insert the data and that data to be saved in database
问题描述
我正在使用网格视图在asp.net中执行应用程序并将数据保存到数据库中。
在GridView中我想要以下设计如下;
Sno名称年龄地址
textbox textbox textbox textbox
textbox textbox textbox textbox
textbox textbox textbox textbox
AddNewRow Insert
(按钮)(按钮)
我想要什么,当我舔AddNewRow(Button)在gridview中添加另一行,当我单击Insert(按钮)时要将保存的数据保存在数据库中。
为此我想做代码并发送源代码。请帮助我。
您可以按照以下步骤操作:
1.在ASPX页面中创建GridView
< asp:gridview id = GridView1 runat = server autogeneratecolumns < span class =code-keyword> = false xmlns:asp = #unknown >
AllowSorting =True>
< 列 >
< asp:templatefield headertext = SNO < span class =code-keyword>>
< edititemtemplate >
< asp:textbox id = txtSNO runat = server text = <% #Bind( SNO)%> > < / asp:textbox >
< span class =code-keyword>< / edititemtemplate >
< itemtemplate >
< asp:label < span class =code-attribute> id = lblSNO runat = server text = <% #Bind( SNO)%> > < / asp:label >
< / itemtemplate >
< / asp:templatefield >
< asp:templatefield headertext = 名称 >
< edititemtemplate >
< asp:textbox id = txtName runat = server text < span class =code-keyword> =<% #Bind( 名称)%> > < / asp:textbox >
< ; / edititemtemplate >
< itemtemplate >
< asp:label id = lblName runat = 服务器 text = <% #Bind( 名称)%> > < / asp:label >
< / itemtemplate >
< / asp:templatefield >
< asp:templatefieldheadertext = 年龄 >
< edititemtemplate >
< asp:textbox id = txtAge runat = server text = <% #Bind( 年龄)%> > < / asp:textbox >
< / edititemtemplate >
< itemtemplate >
< < span class =code-leadattribute> asp:label id = lblAge runat = server text = <% #Bind( 年龄)%> > < / asp:label >
< / itemtemplate >
< / asp:templatefield >
< asp:templatefield headertext = 地址 >
< edititemtemplate >
< asp:textbox id = txtAddress runat = 服务器 text = <% #Bind( 地址)%> > < / asp:textbox >
< / edititemtemplate >
< itemtemplate >
< asp:label id = < span class =code-keyword> lblAddress runat = server text = <% #Bind ( 地址)%> > < ; / asp:label >
< / itemtemplate >
< / asp:templatefield >
< span class =code-keyword>< / columns >
< selectedrow style forecolor = #CCFF99 font-bold = 真 >
BackColor =#009999> < / selectedrow style >
< row style forecolor = #003399 backcolor = 白色 > < / row style >
< / asp:gridview >
2.Create Buttons在ASPX页面
< asp :按钮 id = btnAddNewRow runat = server text = AddNewRow onclick = BtnAddNewRowCl ick xmlns:asp = #unknown / < span class =code-keyword>>
< asp:button id = btnInsert runat = server text = 插入 < span class =code-attribute> onclick = BtnInsertClick xmlns:asp = < span class =code-keyword>#unknown / >
3.定义您的数据类型
它应该是可序列化的
[Serializable]
internal class 示例
{
public static 示例CreateTestSample( int i)
{
return new 示例
{
SNO = SNO + i,
地址= Address + i,
Age = i,
Name = 名称 + i,
City = 城市 + i
};
}
public string SNO {获得跨度>; set ; }
public string 名称{ get 跨度>; set ; }
public string 城市{ get 跨度>; set ; }
public int 年龄{ get 跨度>; set ; }
public string 地址{ get 跨度>; set ; }
}
4.定义持久对象
< span class =code-keyword> private List< sample> DataSamples
{
get { return ViewState [ Samples] as List< sample> ;; }
set {ViewState [ Samples ] = 值; }
}
5.初始化GridView控件
< span class =code-keyword> protected void Page_Load( object sender,EventArgs e )
{
// 首次填写GridView
< span class =code-keyword> if (!IsPostBack)
{
FillGrid();
}
}
6.填写GridView控件
< pre lang =C#> private void FillGrid()
{
< span class =code-comment> // 对于测试sapmle代码
DataSamples = CreateDataSamples();
GridView1.DataSource = DataSamples;
GridView1.DataBind();
// TODO:有更多方法可以从db中检索数据
// 例如SqlDataAdapter
/ / SqlConnection cs = new SqlConnection(Data Source = .\\SQLEXPRESS; Initial Catalog = dbName; Integrated Security = TRUE);
// SqlDataAdapter da = new SqlDataAdapter();
// cs.Open();
// da.SelectCommand = new SqlCommand(SELECT * FROM table,cs);
// da.Fill(dg);
}
私人列表< sample> CreateDataSamples()
{
var list = new 列表< sample>() ;
for ( int i = 0 ; i < 10 ; i ++)
list.Add(Sample.CreateTestSample(一世));
return list;
}
7.定义你的AddNewRow行动
< span class =code-keyword> protected void BtnAddNewRowClick( object sender,EventArgs e )
{
var list = CreateDataSamples();
list.Add( new Sample());
GridView1.EditIndex = list.Count - 1 ;
GridView1.DataSource = DataSamples;
GridView1.DataBind();
}
8.定义插入操作
< span class =code-keyword> protected void BtnInsertClick( object sender,EventArgs e )
{
var row = GridView1.Rows [GridView1.EditIndex];
var sno =((TextBox)(row.FindControl( txtSNO跨度>)))的文本。
var name =((TextBox)(row.FindControl( txtName的跨度>)))的文本。
var age = Convert.ToInt32(((TextBox)(row.FindControl( < span class =code-string> txtAge)))。文本);
var address =((TextBox)(row.FindControl( txtAddress跨度>)))的文本。
var newSample = new 示例
{
SNO = sno,
姓名=姓名,
年龄=年龄,
地址=地址
};
// 在持久数据中保存数据
// TODO你应该为更新数据库编写自己的代码
如果(GridView1.EditIndex > 0 )
DataSamples [GridView1.EditIndex] = newSample ;
GridView1.EditIndex = -1;
GridView1.DataSource = DataSamples;
GridView1.DataBind();
}
如果您需要更多详细信息,请与我们联系。
查看这些CP文章
插入,更新,使用Gridview删除...简单方法 [ ^ ]
CodeProject常见问题系列1:ASP.NET GridView [ ^ ]
I am doing the application in asp.net using grid view and save that data into the data base.
In GridView i want the following design as follows;
Sno Name Age Address
textbox textbox textbox textbox
textbox textbox textbox textbox
textbox textbox textbox textbox
AddNewRow Insert
(Button) (button)
I want when i olick the AddNewRow (Button) another row to be added in the gridview and when i click the Insert (Button) the inserted data to be saved in the database.
for that i want to do the code and send the source code also.please help me.
You can follow these steps:
1.Create GridView in ASPX Page
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="false" xmlns:asp="#unknown"> AllowSorting="True"> <columns> <asp:templatefield headertext="SNO"> <edititemtemplate> <asp:textbox id="txtSNO" runat="server" text="<%# Bind("SNO") %>"></asp:textbox> </edititemtemplate> <itemtemplate> <asp:label id="lblSNO" runat="server" text="<%# Bind("SNO") %>"></asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="Name"> <edititemtemplate> <asp:textbox id="txtName" runat="server" text="<%# Bind("Name") %>"></asp:textbox> </edititemtemplate> <itemtemplate> <asp:label id="lblName" runat="server" text="<%# Bind("Name") %>"></asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="Age"> <edititemtemplate> <asp:textbox id="txtAge" runat="server" text="<%# Bind("Age") %>"></asp:textbox> </edititemtemplate> <itemtemplate> <asp:label id="lblAge" runat="server" text="<%# Bind("Age") %>"></asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="Address"> <edititemtemplate> <asp:textbox id="txtAddress" runat="server" text="<%# Bind("Address") %>"></asp:textbox> </edititemtemplate> <itemtemplate> <asp:label id="lblAddress" runat="server" text="<%# Bind("Address") %>"></asp:label> </itemtemplate> </asp:templatefield> </columns> <selectedrowstyle forecolor="#CCFF99" font-bold="True"> BackColor="#009999"></selectedrowstyle> <rowstyle forecolor="#003399" backcolor="White"></rowstyle> </asp:gridview>
2.Create Buttons in ASPX Page
<asp:button id="btnAddNewRow" runat="server" text="AddNewRow" onclick="BtnAddNewRowClick" xmlns:asp="#unknown" /> <asp:button id="btnInsert" runat="server" text="Insert" onclick="BtnInsertClick" xmlns:asp="#unknown" />
3.Define your DataType
it should be Serializable
[Serializable] internal class Sample { public static Sample CreateTestSample(int i) { return new Sample { SNO = "SNO " + i, Address = "Address " + i, Age = i, Name = "Name " + i, City = "City " + i }; } public string SNO { get; set; } public string Name { get; set; } public string City { get; set; } public int Age { get; set; } public string Address { get; set; } }
4.Define Persistent Object
private List<sample> DataSamples { get { return ViewState["Samples"] as List<sample>; } set { ViewState["Samples"] = value; } }
5.Initialize GridView control
protected void Page_Load(object sender, EventArgs e) { // Fill GridView for First Time if (!IsPostBack) { FillGrid(); } }
6.Fill GridView control
private void FillGrid() { // For test sapmle code DataSamples = CreateDataSamples(); GridView1.DataSource =DataSamples ; GridView1.DataBind(); //TODO:There are more ways to retreiving data from db // e.g SqlDataAdapter // SqlConnection cs = new SqlConnection("Data Source=.\\SQLEXPRESS; Initial Catalog=dbName; Integrated Security=TRUE"); // SqlDataAdapter da = new SqlDataAdapter(); // cs.Open(); // da.SelectCommand = new SqlCommand("SELECT * FROM table", cs); // da.Fill(dg); } private List<sample> CreateDataSamples() { var list = new List<sample>(); for (int i = 0; i < 10; i++) list.Add(Sample.CreateTestSample(i)); return list; }
7.Define your AddNewRow Action
protected void BtnAddNewRowClick(object sender, EventArgs e) { var list = CreateDataSamples(); list.Add(new Sample()); GridView1.EditIndex = list.Count - 1; GridView1.DataSource = DataSamples; GridView1.DataBind(); }
8.Define your Insert Action
protected void BtnInsertClick(object sender, EventArgs e) { var row = GridView1.Rows[GridView1.EditIndex]; var sno = ((TextBox) (row.FindControl("txtSNO"))).Text; var name = ((TextBox) (row.FindControl("txtName"))).Text; var age = Convert.ToInt32(((TextBox) (row.FindControl("txtAge"))).Text); var address = ((TextBox) (row.FindControl("txtAddress"))).Text; var newSample = new Sample { SNO = sno, Name = name, Age = age, Address = address }; //Save Data in Persistent Data //TODO you should write your own code for update DB if (GridView1.EditIndex > 0) DataSamples[GridView1.EditIndex] = newSample; GridView1.EditIndex = -1; GridView1.DataSource = DataSamples; GridView1.DataBind(); }
please let me know if you need anymore details.
Check these CP articles
Insert, Update, Delete with Gridview ... Simple Way[^]
CodeProject Frequently Asked Questions Series 1: The ASP.NET GridView[^]
这篇关于在GridView中如何插入要保存在数据库中的数据和数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!