0

読み取り、削除、編集、更新したいグリッドビューがあり、すべてのメソッドが正しく設定されています。行を選択して削除を実行できるように、グリッド ビューを有効にする方法を知る必要があります。これが私のコードです:

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

あなたは間違った方向にいるようです。別のイベントでselectedIndexのを取得する方法がわかりません。gridviewbuttonclick

グリッド ビューで挿入、更新、および削除を実行する方法については、次の記事をご覧ください。

ASP.NET の GridView の例

Gridview を使用した挿入、更新、削除....簡単な方法

挿入、編集、更新、削除で GridView を使用する方法 Ado.net の方法 C#

これがあなたを正しい方向に導くことを願っています

于 2012-05-11T14:15:08.270 に答える
0

クリックで行を選択するには、次のようなものを使用できます。

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 に答える