locked
Show only first word in gridview RRS feed

  • Question

  • User-807418713 posted

    Hello

    This is my gridview data

    Order Date Order No Customer Order For Color
    25-Jul-19 A1 Gopi Traders BULK Black
    25-Jul-19 A2 Gopi Traders BULK Blue
    25-Jul-19 A3 ABCXYZ PVT LTD BULK Cherry
    25-Jul-19 T5 KA International BULK Black

    I want like this below

    Order Date Order No Customer Order For Color
    25-Jul-19 A1 Gopi BULK Black
    25-Jul-19 A2 Gopi  BULK Blue
    25-Jul-19 A3 ABCXYZ BULK Cherry
    25-Jul-19 T5 KA BULK Black

    how to hide words after space

    thank you

    Tuesday, October 22, 2019 7:52 AM

Answers

  • User288213138 posted

    Hi Gopi.MCA,

    can we do this same using gridviewdatabound

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 23, 2019 2:39 AM

All replies

  • User288213138 posted

    Hi Gopi.MCA,

    how to hide words after space

    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

    Tuesday, October 22, 2019 9:35 AM
  • User-807418713 posted

    Hello

    Thanks for your code

    can we do this same using gridviewdatabound

    thank you

    Tuesday, October 22, 2019 9:53 AM
  • User409696431 posted

    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.

    Tuesday, October 22, 2019 5:25 PM
  • User288213138 posted

    Hi Gopi.MCA,

    can we do this same using gridviewdatabound

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 23, 2019 2:39 AM
  • User-1716253493 posted
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (!string.IsNullOrEmpty(e.Row.Cells[2].Text))
                {
                    e.Row.Cells[2].Text = e.Row.Cells[2].Text.Split(' ').First();
                }
            }
        }

    Wednesday, October 23, 2019 3:12 AM
  • User-1038772411 posted

    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

    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <asp:GridView ID="GridView1" HeaderStyle-BackColor="#5151a0" HeaderStyle-ForeColor="#ffffff"
        runat="server" AutoGenerateColumns="false" OnDataBound = "OnDataBound" OnRowDataBound="GridView1_RowDataBound">   
        <Columns>        
            <asp:BoundField DataField="Section1Data1" HeaderText="Data1" ItemStyle-Width="150" />
            <asp:BoundField DataField="Section1Data2" HeaderText="Data2" ItemStyle-Width="150" />
            <asp:BoundField DataField="Section2Data1" HeaderText="Data1" ItemStyle-Width="150" />
            <asp:BoundField DataField="Section2Data2" HeaderText="Data2" ItemStyle-Width="150" />
        </Columns>
    </asp:GridView>
    </asp:Content>

    .cs page

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Section1Data1"), new DataColumn("Section1Data2"), new DataColumn("Section2Data1"), new DataColumn("Section2Data2") });
                    List<string> customerList = new List<string>();
                    customerList.Add("Gopi Traders");
                    customerList.Add("Gopi Traders");
                    customerList.Add("ABCXYZ PVT LTD");
                    customerList.Add("KA International");
                    customerList.Add("");
                    customerList.Add(null);
                    customerList.Add("GopiTraders");
    
                    for (int i = 0; i <= 6; i++)
                    {
                       
                        dt.Rows.Add(customerList[i], "Country " + i, "EName " + i, "ECountry " + i);
                    }                
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
    
            protected void OnDataBound(object sender, EventArgs e)
            {
                GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
                TableHeaderCell cell = new TableHeaderCell();
                
                cell = new TableHeaderCell();
                cell.Text = "Customers";
                cell.ColumnSpan = 2;
                row.Controls.Add(cell);
    
                cell = new TableHeaderCell();
                cell.ColumnSpan = 2;
                cell.Text = "Employees";
                row.Controls.Add(cell);
    
                row.BackColor = ColorTranslator.FromHtml("#d9aaed");
                GridView1.HeaderRow.Parent.Controls.AddAt(0, row);
    
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Cells[0].Text = string.IsNullOrEmpty(e.Row.Cells[0].Text) || e.Row.Cells[0].Text == "&nbsp;" ? "N/A" : e.Row.Cells[0].Text.Split(' ').First();             
                }
            }

    If you want any other help then please let us know.

    Thanks.

    Wednesday, October 23, 2019 1:19 PM