如何将GridView列类型从TextBox更改为LinkButton

本文关键字:TextBox LinkButton 类型 GridView | 更新日期: 2023-09-27 18:20:17

我需要知道如何允许Gridview列接受LinkButton。默认情况下,GridViewColumn设置为接受TextBox控件,但我需要一个LinkButton控件。我的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;
using System.Data.Common;
using System.Globalization;

    namespace GridView_Tutorial
    {
        public partial class GridView : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                SqlConnection con = new SqlConnection("Data Source=Mehdi-PC''SqlExpress; Initial Catalog=PIMS; Integrated Security=true");
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("SELECT cat_id, cat_name FROM quest_categories", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                //add a blank row to returned DataTable 
                DataRow dr = dt.NewRow();
                dt.Rows.InsertAt(dr, 0);
                //creating the first row of Gridview to be editable
                GridView1.EditIndex = 0;
                //Data Sourcing and binding
                GridView1.DataSource = dt;
                GridView1.DataBind();
                //Changing the Text for Inserting a New Record
                ((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert";
                if (con != null)
                {
                    con.Close();
                }
            }
        }
    }

以下代码行抛出错误:

((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert"; 

错误消息为:

InvalidCastException is unhandled by the user.
Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.Web.UI.WebControls.LinkButton'.

当我更改代码时:

((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert"; 

((TextBox)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert"; 

错误消失,代码在数据表中插入一个文本框。

请帮我插入一个链接按钮而不是文本框。

问候

如何将GridView列类型从TextBox更改为LinkButton

请尝试使用以下代码片段。

ASPX-

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

ASPX.CS

protected void Page_Load(object sender, System.EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Shipper", typeof(string));
    dt.Columns.Add("ShipperTemp", typeof(string));
    dt.Rows.Add("CShipper1", "1");
    dt.Rows.Add("BShipper2", "2");
    dt.Rows.Add("AShipper1", "1");
    dt.Rows.Add("EShipper1", "2");
    dt.Rows.Add("DShipper4", "4");
    DataRow dr = dt.NewRow();
    dt.Rows.InsertAt(dr, 0);
    //creating the first row of Gridview to be editable
    GridView1.EditIndex = 0;
    //Data Sourcing and binding
    GridView1.DataSource = dt;
    GridView1.DataBind();
    //Changing the Text for Inserting a New Record
    //((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert";
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit)
    {
        e.Row.Cells[0].Controls.Clear(); // Comment this line if you do not want to hide Textbox from First Cell first Row
        LinkButton btn = new LinkButton();
        btn.ID = "ID";
        btn.Text = "Insert";
        e.Row.Cells[0].Controls.Add(btn);
    }
}