如何将GridView Column类型从TextBox更改为LinkBut​​ton [英] How to change GridView Column type from TextBox to LinkButton

查看:70
本文介绍了如何将GridView Column类型从TextBox更改为LinkBut​​ton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要知道如何将GridView Column类型从Textbox更改为LinkBut​​ton,或者找到另一种方法在第一个单元格中有一个链接按钮DataGrid中的第一行。



例如,我的代码是:



< pre lang = c#>使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Data;
使用System.Data.SqlClient;
使用System.Data.OleDb;
使用System.IO;
使用System.Data.Common;
使用System.Globalization;


命名空间GridView_Tutorial
{
公共部分类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 (数据源= Mehdi-PC \\SqlExpress;初始目录= PIMS;集成安全性=真);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(SELECT cat_id,cat_name FROM quest_categories,con);
DataTable dt = new DataTable();
da.Fill(dt);
//为返回的DataTable添加一个空行
DataRow dr = dt.NewRow();
dt.Rows.InsertAt(dr,0);
//创建第一行Gridview可编辑
GridView1.EditIndex = 0;

//数据采购和绑定
GridView1.DataSource = dt;
GridView1.DataBind();
//更改用于插入新记录的文本
((TextBox)GridView1.Rows [0] .Cells [0] .Controls [0])。Text =Insert;
if(con!= null)
{
con.Close();
}
}
}
}





而不是



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



我需要一个LinkBut​​ton ..这样



((LinkBut​​ton)GridView1.Rows [0] .Cells [0] .Controls [0 ])。Text =Insert;



当我尝试从TextBox更改为LinkBut​​ton时,我得到的错误是:



InvalidCastException未被用户处理。

无法将类型为System.Web.UI.WebControls.TextBox的对象强制转换为'System.Web.UI.WebControls'。 LinkBut​​ton'。



请帮忙。谢谢

解决方案

您可以在网格视图属性中将文本框更改为linkbutton。

可以提供数据网格的代码..

Hi All,

I need to know how to change GridView Column type from Textbox to LinkButton or find an alternate way to have a link button in the first cell of the top row in DataGrid.

For example, my code is:

<pre lang="c#">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
            ((TextBox)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert";
            if (con != null)
            {
                con.Close();
            }
        }
    }
}



Instead of

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

I need to have a LinkButton .. such that

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

When I try to change from TextBox to LinkButton, error that I get is:

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

Please help. Thanks

解决方案

You can change the textbox to linkbutton in grid view property.
can you provide the code of your datagrid..


这篇关于如何将GridView Column类型从TextBox更改为LinkBut​​ton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆