Hello All,
I am getting data row column value as "Anand(sometext)" as my firstname , but here i need to remove or hide text after "Anand"
I am using only data row binded with datatable. please help me on this to achive below task.
Eample : Anand(hello) I need output should be only Anand need to display in Grid.

Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 11, 2023, 12:27 AM EDT
Try below:
//Assuming that "dataTable" is the DataTable you're using to bind to the grid //Create a new column "FirstNameWithoutText" in the DataTable dataTable.Columns.Add("FirstNameWithoutText", typeof(string)); //Loop through each row in the DataTable foreach(DataRow row in dataTable.Rows) { //Get the value of the "FirstName" column string firstName = row["FirstName"].ToString(); //Find the index of the opening parenthesis int openingParenIndex = firstName.IndexOf("("); //If the opening parenthesis is found, remove the text in parentheses if(openingParenIndex != -1) { firstName = firstName.Substring(0, openingParenIndex).Trim(); } //Set the value of the "FirstNameWithoutText" column row["FirstNameWithoutText"] = firstName; } //Bind the modified DataTable to the grid yourGrid.DataSource = dataTable; yourGrid.DataBind();Thanks
Amit MohantyRank #112 · 20K reputationPosted Apr 11, 2023, 12:03 AM EDT
To remove the text after "Anand" in your data row column value, you can use a string manipulation function such as
substringorsplitstring input = "Anand(hello)"; string output = input.Substring(0, input.IndexOf('(')); Console.WriteLine(output); // Output: "Anand"string input = "Anand(hello)"; string[] parts = input.Split('('); string output = parts[0]; Console.WriteLine(output); // Output: "Anand"Tuhin PaulRank #7 · 71.9K reputationPosted Apr 29, 2023, 10:29 PM EDT
1. Before accessing the "FirstName" column from the row, it's better to check if the value is not null. You can add an extra check like this:
if(row["FirstName"] != null && !string.IsNullOrEmpty(row["FirstName"].ToString())) { string firstName = row["FirstName"].ToString(); ... }2. Instead of using IndexOf and Substring methods to remove the text inside the parentheses, you can use the Split method to split the string into two parts, using the opening parenthesis as a delimiter. Then, you can take the first part of the resulting array, which will contain the first name without the text inside the parentheses. Here's how you can do this:
string firstName = row["FirstName"].ToString(); int openingParenIndex = firstName.IndexOf("("); if(openingParenIndex != -1) { firstName = firstName.Split('(')[0].Trim(); } row["FirstNameWithoutText"] = firstName;3. Use ternary operator for null checks: Instead of using an if statement to check for null values, you can use the ternary operator to make the code more concise. Here's how the final code would look like:
dataTable.Columns.Add("FirstNameWithoutText", typeof(string)); foreach(DataRow row in dataTable.Rows) { string firstName = row["FirstName"] != null ? row["FirstName"].ToString() : ""; int openingParenIndex = firstName.IndexOf("("); firstName = openingParenIndex != -1 ? firstName.Split('(')[0].Trim() : firstName; row["FirstNameWithoutText"] = firstName; } yourGrid.DataSource = dataTable; yourGrid.DataBind();These improvements will make accepted code more concise, readable, and robust.