0

I have a gridview that I want to read, delete, edit and update, I have all the methods set up correctly. I need to know how to enable the grid view so I can select a row to then invoke the delete. Here is my code:

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();
    }
}
}
4

2 回答 2

0

Seems like you are in wrong direction. Not sure how you going to obtain the selectedIndex of the gridview in a separate buttonclick event.

To get an idea to how to perform insert, update and delete in a grid-view, have a look at these article about

GridView Examples for ASP.NET

Insert, Update, Delete with Gridview....simple way

How to use GridView with Insert, Edit, Update, Delete the Ado.net way C#

Hope this might direct you in correct direction

于 2012-05-11T14:15:08.270 回答
0

To select a row onclick, you can use something like this:

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()))
    }    
}
于 2012-05-11T14:23:14.573 回答