2011-01-21 19 views
0
public partial class Gridvw_expt2 : System.Web.UI.Page 
{ 
    SqlCommand com; 
    SqlDataAdapter da; 
    DataSet ds; 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["gj"].ConnectionString); 
    protected void Page_Load(object sender, EventArgs e) 
    {   
     com = new SqlCommand("Select * from tblExpt",con); 
     da = new SqlDataAdapter(com); 
     ds = new DataSet(); 
     da.Fill(ds); 
     if (ds.Tables[0].Rows[0] != null) 
     { 
      GridView1.AutoGenerateEditButton = true; 

      GridView1.DataSource = ds; 
      GridView1.DataBind(); 

      GridView1.RowUpdating += new GridViewUpdateEventHandler(abc); 
      GridView1.DataKeyNames = new string[] { "id" }; 
      GridView1.RowEditing += new GridViewEditEventHandler(bc); 
     }  
     else 
      Response.Write("fkj"); 
    } 

    protected void abc(object sender, GridViewUpdateEventArgs e) 
    { 
     Response.Write(e.RowIndex); 
    } 
    protected void bc(object sender, GridViewEditEventArgs e) 
    { 
     GridView gv = (GridView)sender; 
     gv.EditIndex = e.NewEditIndex; 
    } 
} 

次の行を編集する場合にのみ、編集モードで取得するために使用される行は、最初の行が編集モードでは決して取得されないことを意味します。イベントを動的に更新するgridviewを作成するにはどうすればよいですか?

+0

正確なコンパイルメッセージ? –

+0

GridView 'GridView1'発生したイベントRowEditingが処理されていません – Learner

答えて

2

Isteadは、次の操作を行います。また

GridView1.RowUpdating += new GridViewUpdateEventHandler(abc); 

を、代わりに

GridView1.Attributes.Add("DataKeyNames", "id"); 

は、代わりに

if (ds.Tables[0].Rows[0].ToString() != null) 

の、また×2本

GridView1.DataKeyNames = new string[] { "id" }; 

を行い、私はクラスを教えているように、なぜ私は感じています。この

if (ds.Tables[0].Rows[0] != null) //.ToString() will cause an exception if it is actuall null 

を行います:)

+0

ありがとう、同じ例外がまだそこにあります – Learner

+0

あなたのアプローチは理想的ではありません..あなたはaspxファイルからイベントハンドラを接続する必要があります..例外はおそらく間違ったポストバック処理..私は本を探すことをお勧めします。 –

0

これは、GridView.RowEditingを処理するハンドラを設定していないためです。グリッドビュー(.aspx内)で、RowEditingを処理するメソッドを配線する必要があります。

あなたはGridViewのコードは次のようになりますよ:あなたが追加する必要があり

<asp:GridView ID="GridView1" runat="server"> 
</asp:GridView> 

nameOfMethodYouWantToFireはあなたのコードである
<asp:GridView ID="GridView1" runat="server" OnRowEditing="nameOfMethodYouWantToFire"> 
</asp:GridView> 

OnRowEditing="nameOfMethodYouWantToFire" 

をので、それがどのように見えますあなたのC#の後ろにあり、イベントを処理します。このような何か:

GridView1.Attributes.Add("onrowupdating", "abc"); 

protected void nameOfMethodYouWantToFire(object sender, GridViewPageEventArgs e) 
    { 
    //Your code here 
    } 
+0

返信いただきありがとうございます。今後の返信をお寄せいただきありがとうございます。 – Learner

+0

あなたは何を意味するのかよく分かりません。これはGridViewが動的に移植されているかどうかにかかわらず動作します –

関連する問題