2016-11-30 129 views
0

我有3個任務:爲什麼GRIDVIEW記錄在單個gridview中顯示兩次?

  1. 記錄不應該重複顯示像那個圖像。 output
  2. 當我按一下按鈕,它具有獲取整行記錄,該特定行的索引,
  3. 點擊該按鈕後,我的下一行,其中用戶點擊按鈕中插入一些記錄。

例如:gridview中共有10行記錄。如果用戶單擊第二行按鈕,它必須獲取整行的詳細信息,行的索引值,然後從第三行插入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); 
    } 

最重要的事情是:我應該得到該行的索引和插入的記錄兩個記錄。

+0

你能解決你的問題嗎? –

回答

0

對於您的重複記錄問題:GridView控件上有一個屬性,名爲AutoGenerateColumns。它默認爲true

如果爲true,則數據源中的所有列/字段/屬性將自動顯示爲結果中的列。如果添加了列,那麼除了自動生成的列之外(以及之後),這些列將會出現。

這很容易解決:在AutoGenerateColumns屬性更改爲false

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

如果有其他問題發生,請爲那些獨立的問題。