In this article I will explain how to show limited characters in gridview using asp.net.
Description:
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
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Gridview With Limited Characters</title> </head> <body> <form id="form1" runat="server"> <table cellpadding="0" cellspacing="0" width="300px" align="center"> <tr> <td> <asp:GridView ID="GridView1" runat="server" Width="100%" CssClass="feedbacklink" AutoGenerateSelectButton="True" onrowdatabound="GridView1_RowDataBound" PageSize="5" > </asp:GridView> </td> </tr> <tr> <td></td> </tr> <tr> <td> <asp:TextBox ID="txtmsg" runat="server" TextMode="MultiLine" CssClass="textarea" Width="300px" ></asp:TextBox></td> </tr> </table> </form> </body> </html> |
string strConn = "Data Source=MYSystem;Initial Catalog=MySamplesDB;Integrated Security=True"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) bindGrid(); } 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); myDataAdapter.Fill(myDataSet); GridView1.DataSource = myDataSet; GridView1.DataBind(); // GridView1.Columns[3].Visible = false; } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { int i = 0; e.Row.Cells[3].Visible = false; if (e.Row.RowType == DataControlRowType.DataRow) { foreach (TableCell cell in e.Row.Cells) { i++; string s = cell.Text; if (cell.Text.Length > 25 && (i == 3)) cell.Text = cell.Text.Substring(0, 25) + "...."; cell.ToolTip = s; } } } |
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|


Subscribe by RSS
Subscribe by Email
8 comments :
I'm getting error as
e.Row.Cells[3].Visible = false; ====> Error:Specified argument was out of range of valid vavalues parameter:Index
Excelent!!!
Works fine!!!!
Thanks!
Work like a charm!!!
Thanks!
but when Export to excel required then what to do?
It Export with "......" not whole data
what is "!IsPostBack"
I have followed all your code, and in my database, I added the 3rd column is id, so when my code has changed the cell [2] cell [3] but did not show up "...."as in code which instead show all the data in the column description into one long line. In my database is: id, subject, description.Please help me!!!!
Nat chaal tu.....................
Thank's