2016-11-30 17 views
0

私は3つのタスクを持っています:GRIDVIEWレコードが1回のグリッドビューで2回表示されている理由を教えてください。

  1. レコードはそのイメージのように表示されるべきではありません。私はボタンをクリックするとoutput
  2. 、それは、そのボタンをクリックした後
  3. 、行全体を記録し、その特定の行のインデックスを取得するために持って、私は、ユーザーがボタンをクリックし、次の行の一部のレコードを挿入する必要があります。

例:完全に10行のレコードが、gridviewにあります。ユーザーが2行目のボタンをクリックすると、行の詳細全体を取得し、行のインデックス値を取得し、3行目から5行5行を挿入する必要があります。

これは私のコードです: Sample.aspx

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

Sample.aspx.cs

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!this.IsPostBack) 
     { 
      BoundField bfield = new BoundField(); 
      bfield.HeaderText = "Name"; 
      bfield.DataField = "Name"; 
      GridView1.Columns.Add(bfield); 

      TemplateField tfield = new TemplateField(); 
      tfield.HeaderText = "Country"; 
      GridView1.Columns.Add(tfield); 

      tfield = new TemplateField(); 
      tfield.HeaderText = "Id"; 
      GridView1.Columns.Add(tfield); 
      //BindGrid(); 
     } 
     this.BindGrid(); 
    } 

private void BindGrid() 
    { 
     DataTable dt = new DataTable(); 
     dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)), 
        new DataColumn("Name", typeof(string)), 
        new DataColumn("Country",typeof(string)) }); 
     dt.Rows.Add(1, "John Hammond", "United States"); 
     dt.Rows.Add(2, "Mudassar Khan", "India"); 
     dt.Rows.Add(3, "Suzanne Mathews", "France"); 
     dt.Rows.Add(4, "Robert Schidner", "Russia"); 
     GridView1.DataSource = dt; 
     GridView1.DataBind(); 
    } 

    protected void btnRefresh_Click(object sender, EventArgs e) 
    { 

    } 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      TextBox txtCountry = new TextBox(); 
      txtCountry.ID = "txtCountry"; 
      txtCountry.Text = (e.Row.DataItem as DataRowView).Row["Country"].ToString(); 
      e.Row.Cells[1].Controls.Add(txtCountry); 

      Button lnkView = new Button(); 
      lnkView.ID = "lnkView"; 
      lnkView.Text = (e.Row.DataItem as DataRowView).Row["Id"].ToString(); 
      lnkView.Click += ViewDetails; 
      lnkView.CommandArgument = (e.Row.DataItem as DataRowView).Row["Id"].ToString(); 
      e.Row.Cells[2].Controls.Add(lnkView); 
     } 
    } 

    protected void ViewDetails(object sender, EventArgs e) 
    { 
     Button lnkView = (sender as Button); 
     GridViewRow row = (lnkView.NamingContainer as GridViewRow); 
     string id = lnkView.CommandArgument; 
     string name = row.Cells[0].Text; 
     string country = (row.FindControl("txtCountry") as TextBox).Text; 
     ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Id: " + id + " Name: " + name + " Country: " + country + "')", true); 
    } 

最も重要なことは、次のとおりです。私はその行のインデックスを取得し、間にレコードを挿入する必要があり2つのレコード。

+0

問題を解決できましたか? –

答えて

0

重複しているレコードの懸念について:GridViewのコントロールには、AutoGenerateColumnsという名前のコントロールがあります。デフォルトはtrueです。

これがtrueの場合、データソース内のすべての列/フィールド/プロパティは自動的に結果として列として表示されます。列を追加すると、それらの列は自動的に生成された列に加えて(および後に)表示されます。

これは修正するのは簡単です:その他の問題が発生している場合は、

<asp:GridView ID="GridView1" runat="server" 
    OnRowDataBound="GridView1_RowDataBound"  
    AutoGenerateColumns="false"></asp:GridView> 

をそれらのための別の質問をする:falseAutoGenerateColumnsプロパティを変更します。

関連する問題