我有一个想要阅读,删除,编辑和更新的网格视图,我已正确设置所有方法。我需要知道如何启用网格视图,以便我可以选择一行然后调用删除。这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace ConsumeDSetService
{
public partial class _Default : System.Web.UI.Page
{
public static DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
localhost.Service1 myws = new localhost.Service1();
ds = myws.GetDataSet();
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
//updates database in web service with contents of dataset
localhost.Service1 myws = new localhost.Service1();
Label7.Text = myws.ModifyDatabase(ds);
}
protected void Button3_Click(object sender, EventArgs e)
{
//reads student record after row in grid view has been selected
int i = GridView1.SelectedIndex;
TextBox1.Text = System.Convert.ToString(ds.Tables["Students"].Rows[i]["RollNumber"]);
TextBox2.Text = System.Convert.ToString(ds.Tables["Students"].Rows[i]["FirstName"]);
TextBox3.Text = System.Convert.ToString(ds.Tables["Students"].Rows[i]["Surname"]);
TextBox4.Text = System.Convert.ToString(ds.Tables["Students"].Rows[i]["Course"]);
TextBox5.Text = System.Convert.ToString(ds.Tables["Students"].Rows[i]["Level"]);
TextBox6.Text = System.Convert.ToString(ds.Tables["Students"].Rows[i]["Address"]);
}
protected void Button4_Click(object sender, EventArgs e)
{
//inserts new row into database
DataRow dr = ds.Tables["Students"].NewRow();
dr["RollNumber"] = TextBox1.Text;
dr["FirstName"] = TextBox2.Text;
dr["Surname"] = TextBox3.Text;
dr["Course"] = TextBox4.Text;
dr["Level"] = TextBox5.Text;
dr["Address"] = TextBox6.Text;
ds.Tables["Students"].Rows.Add(dr);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button5_Click(object sender, EventArgs e)
{
//edits data row in set
int i = GridView1.SelectedIndex;
ds.Tables["Students"].Rows[i]["RollNumber"] = TextBox1.Text;
ds.Tables["Students"].Rows[i]["FirstName"] = TextBox2.Text;
ds.Tables["Students"].Rows[i]["Surname"] = TextBox3.Text;
ds.Tables["Students"].Rows[i]["Course"] = TextBox4.Text;
ds.Tables["Students"].Rows[i]["Level"] = TextBox5.Text;
ds.Tables["Students"].Rows[i]["Address"] = TextBox6.Text;
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button6_Click(object sender, EventArgs e)
{
//deletes row in set
int i = GridView1.SelectedIndex;
ds.Tables["Students"].Rows[i].Delete();
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
答案 0 :(得分:0)
好像你的方向错了。不确定如何在单独的selectedIndex
事件中获取gridview
的{{1}}。
要了解如何在网格视图中执行插入,更新和删除,请查看这些文章
Insert, Update, Delete with Gridview....simple way
How to use GridView with Insert, Edit, Update, Delete the Ado.net way C#
希望这可能会指引你正确的方向
答案 1 :(得分:0)
要选择一行onclick,您可以使用以下内容:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(Me.GridView1, "Select$" + e.Row.RowIndex.ToString()))
}
}