Extend BoundField Of GridView


.
In this post i will show you how to extend BoundField Column of Gridview to add MultiLine support
Step 1.Place the code in app_code directory


using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomBoundField
{
public class BoundTextBoxField : System.Web.UI.WebControls.BoundField
{

public TextBoxMode TextMode
{
get
{
TextBoxMode _tm = TextBoxMode.SingleLine;
if (this.ViewState["TextMode"] != null)
_tm = (TextBoxMode)this.ViewState["TextMode"];
return _tm;
}
set { this.ViewState["TextMode"] = value; }
}

public int Columns
{
get
{
int i = 0;
if (this.ViewState["Columns"] != null)
i = (int)this.ViewState["Columns"];
return i;
}
set { this.ViewState["Columns"] = value; }
}

public int Rows
{
get
{
int i = 0;
if (this.ViewState["Rows"] != null)
i = (int)this.ViewState["Rows"];
return i;
}
set { this.ViewState["Rows"] = value; }
}

public bool Wrap
{
get
{
bool b = true;
if (this.ViewState["Wrap"] != null)
b = (bool)this.ViewState["Wrap"];
return b;
}
set { this.ViewState["Wrap"] = value; }
}

protected override void OnDataBindField(object sender, EventArgs e)
{
base.OnDataBindField(sender, e);
Control c = (Control)sender;
if (c is TextBox)
{
TextBox txt = (TextBox)c;
txt.TextMode = this.TextMode;
txt.Columns = this.Columns;
txt.Rows = this.Rows;
txt.Wrap = this.Wrap;
}
}

}

}

Step2. Register the Control in asp page

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<%@ Register Namespace="CustomBoundField" TagPrefix="custom"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView AutoGenerateColumns="false" ID="GridView1" runat="server" AutoGenerateEditButton="True" OnRowEditing="GridView1_RowEditing">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Single Line" />
<custom:boundtextboxfield datafield="Name" headertext="MultiLine Text" sortexpression="Text"
textmode="MultiLine" rows="5" columns="25" wrap="True" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Default.aspx.cs


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}

}
private void BindGrid()
{
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}
protected DataTable GetDataSource()
{

const string key = "MyDataSource";
DataTable dt = Session[key] as DataTable;

if (dt == null)
{

dt = new DataTable();
dt.Columns.Add("ID", typeof(int));

dt.Columns.Add("Name", typeof(string));
dt.Rows.Add("1", "first object");

dt.Rows.Add("2", "second object");
dt.Rows.Add("3", "second object");
dt.Rows.Add("4", "second object");
Session[key] = dt;

}

return dt;
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();

}

}


1 comment: