According to your description, i made demo for you. I used the split() method to split data.
You can put the data into the DataTable and then update the value of the customer by traversing.
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT * FROM Test59";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
foreach (DataRow row in dt.Rows)
{
row["Customer"] = row["Customer"].ToString().Split(null)[0];
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
The result:
Best regards,
Sam
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
In the example case in the link above, it's adding html formatting around one of the cell contents. In your case you'd use the String Split for each of the columns where you wanted to modify the text.
Yes, you can bind the data in pageload event, then update the data in the DataBound event.
<asp:GridView ID="GridView1" runat="server" OnDataBound="GridView1_DataBound"></asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT * FROM Test59";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
Session["data"] = dt;
GridView1.DataSource =(DataTable)Session["data"];
GridView1.DataBind();
}
}
}
}
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
DataTable dt1 = (DataTable)Session["data"];
int i = 0;
foreach (DataRow row in dt1.Rows)
{
row["Customer"] = row["Customer"].ToString().Split(null)[0];
GridView1.Rows[i].Cells[1].Text = row["Customer"].ToString();
i += 1;
}
}
The result:
Best regards,
Sam
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
294 Points
679 Posts
Show only first word in gridview
Oct 22, 2019 07:52 AM|Gopi.MCA|LINK
Hello
This is my gridview data
I want like this below
how to hide words after space
thank you
Contributor
3370 Points
1409 Posts
Re: Show only first word in gridview
Oct 22, 2019 09:35 AM|samwu|LINK
Hi Gopi.MCA,
According to your description, i made demo for you. I used the split() method to split data.
You can put the data into the DataTable and then update the value of the customer by traversing.
The result:
Best regards,
Sam
Member
294 Points
679 Posts
Re: Show only first word in gridview
Oct 22, 2019 09:53 AM|Gopi.MCA|LINK
Hello
Thanks for your code
can we do this same using gridviewdatabound
thank you
Contributor
5961 Points
2468 Posts
Re: Show only first word in gridview
Oct 22, 2019 05:25 PM|KathyW|LINK
You can modify the contents in the GridView.RowDataBound event.
See https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview.rowdatabound?view=netframework-4.8 for an example.
In the example case in the link above, it's adding html formatting around one of the cell contents. In your case you'd use the String Split for each of the columns where you wanted to modify the text.
Contributor
3370 Points
1409 Posts
Re: Show only first word in gridview
Oct 23, 2019 02:39 AM|samwu|LINK
Hi Gopi.MCA,
Yes, you can bind the data in pageload event, then update the data in the DataBound event.
The result:
Best regards,
Sam
All-Star
52683 Points
15720 Posts
Re: Show only first word in gridview
Oct 23, 2019 03:12 AM|oned_gk|LINK
Suwandi - Non Graduate Programmer
Participant
850 Points
492 Posts
Re: Show only first word in gridview
Oct 23, 2019 01:19 PM|AddWeb Solution|LINK
Hello Gopi.MCA
I review you want hide words after space.
Here I attached my code for the aspx page and codebehind page.
.aspx page
.cs page
If you want any other help then please let us know.
Thanks.