编辑,删除,在服务器端的网格视图中添加数据
如何添加
How can I add
DataTable dt = new DataTable();
Dal.Fill_Dt("SELECT Tbl_NwsLtr.ID_NwsLtr , Tbl_NwsLtr.FlNm , Tbl_NwsLtr._ml,Tbl_NwsLtr._MbN, Tbl_Job.JbName, Tbl_NwsLtr._dt FROM Tbl_NwsLtr INNER JOIN Tbl_Job ON Tbl_NwsLtr._Jb_ID = Tbl_Job._Jb_ID", dt);
GridView1.DataSource = dt;
GridView1.DataBind();
这是表之间的关系:
< img src =''http:// images.uvl.ir/img/e5eeca522e12.png''alt =''图片由免费照片托管主持http://www.iranxm.com/''/>
当用户点击编辑按钮时,如何在代码中编辑,更新和添加数据behinde
?我想将字段JbName显示到下拉列表中。
我该怎么做?
谢谢
请帮忙!
this is relation between tables:
<img src=''http://images.uvl.ir/img/e5eeca522e12.png'' alt=''Image Hosted by Free Photo Hosting at http://www.iranxm.com/'' />
How can I edit,update,and add data in code behinde
when the user click on the "Edit" button? I want to show field JbName into dropdownlist.
how can I do this?
Thanks
please help!
AutoGenerateColumns="false"
like
like
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
</asp:GridView>
[从第二个解决方案移动]
http://www.aspdotnet-suresh。 com / 2011/02 / how-to-inserteditupdate-and-delete-data.html [ ^ ]
观看此示例代码..
[Moved from 2nd solution]
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html[^]
watch this sample code..
ok首先要创建一个函数,你将从中填充你的gridview:
ok first of all make a function from which u will populate your gridview like this :
public void show()
{
string str = "select * from table2";
SqlDataAdapter adp = new SqlDataAdapter(str, con);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
然后转到你的gridview并点击那里的智能标签选择编辑列并添加一个新的命令编辑,更新和取消
现在转到gridview活动
双击RowEditing活动:
then go to your gridview and click on the smart tag from there select uption edit columns and add a new command feild "edit, update and cancel"
now go to gridview events
double click the RowEditing event:
gridShow.EditIndex = e.NewEditIndex;
show();
同样,
以下RowCancelling事件:
similarly,
under RowCancelling event:
GridView1.EditIndex = -1;
show();
在RowUpdating事件下:
在此之前你应该知道您应该将gridview列转换为仅模板字段转到gridview的智能标记并单击编辑模板,然后对于您不想编辑的列,在他们的edititemtemplate中插入标签,并且要更改的字段添加文本框
under RowUpdating event:
before doing that you should know that you should convert your gridview columns into only template fields go to smart tag of gridview and click edit template and then for the columns you dont want to edit insert a label in their edititemtemplate and the fields you want to change add a textbox
int id =Convert.ToInt32( ((Label)GridView1.Rows[e.RowIndex].FindControl("Label4")).Text);
string userid = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
string pwd = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text;
string str = "update table2 set userid=''" + userid + "'',pwd=''" + pwd + "'' where id=" + id;
SqlCommand cmd = new SqlCommand(str, con);
cmd.Connection = con;
// con.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
GridView1.EditIndex = -1;
show();
删除操作如下:
delete is done like this:
int id = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("Label12")).Text);
string del = "Delete from monthly where id="+id;
SqlCommand cmd = new SqlCommand(del, con);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
show();
只需调整代码就可以了。
just tweak the code and it will work for you.