In this article I will explain how to show limited characters in gridview using asp.net.
I have created one application for that we have prepared one feedback form to collect feedback from users regarding our application for that we have taken gridview to bind all the feedbacks into one page. Here some of the users have given one line of feedback some of the users have given two lines of feedback remaining people have given feedback with bigger matter at that time if we bind all the feedbacks to one gridview at that time gridview size has increased a lot and in some rows matter is small but columns size is bigger that is just like this
For that reason we tried to maintain consistency for entire gridview to maintain consistency for gridview we need to write functionality in Gridview RowDataBound event
Design your aspx page like this
<title>Gridview With Limited Characters</title>
<form id="form1" runat="server">
<table cellpadding="0" cellspacing="0" width="300px" align="center">
<asp:GridView ID="GridView1" runat="server" Width="100%"
onrowdatabound="GridView1_RowDataBound" PageSize="5" >
<asp:TextBox ID="txtmsg" runat="server" TextMode="MultiLine"
CssClass="textarea" Width="300px" ></asp:TextBox></td>
In code behind write this functionality to bind gridview and write functionality in Gridview_Rowdatabound to limit the characters in gridview columns.
string strConn = "Data Source=MYSystem;Initial Catalog=MySamplesDB;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
public void bindGrid()
SqlConnection con = new SqlConnection(strConn);
DataSet myDataSet = new DataSet();
SqlCommand cmd = new SqlCommand("Select * from Feedback", con);
// cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter myDataAdapter = new SqlDataAdapter(cmd);
GridView1.DataSource = myDataSet;
// GridView1.Columns.Visible = false;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
int i = 0;
e.Row.Cells.Visible = false;
if (e.Row.RowType == DataControlRowType.DataRow)
foreach (TableCell cell in e.Row.Cells)
string s = cell.Text;
if (cell.Text.Length > 25 && (i == 3))
cell.Text = cell.Text.Substring(0, 25) + "....";
cell.ToolTip = s;