如何将GridView Column类型从TextBox更改为LinkButton [英] How to change GridView Column type from TextBox to LinkButton
问题描述
大家好,
我需要知道如何将GridView Column类型从Textbox更改为LinkButton,或者找到另一种方法在第一个单元格中有一个链接按钮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;
我需要一个LinkButton ..这样
((LinkButton)GridView1.Rows [0] .Cells [0] .Controls [0 ])。Text =Insert;
当我尝试从TextBox更改为LinkButton时,我得到的错误是:
InvalidCastException未被用户处理。
无法将类型为System.Web.UI.WebControls.TextBox的对象强制转换为'System.Web.UI.WebControls'。 LinkButton'。
请帮忙。谢谢
您可以在网格视图属性中将文本框更改为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更改为LinkButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!