0

I have a table call Reviws in that table I have column call Description so user can add the description. Now what I need is in that description paragraph need to ignore some words and display.

I have some words like

a is about above after again against all

This is the sample paragraph.

Anuradhapura is a major city in Sri Lanka. It is the capital city of North Central Province, Sri Lanka and the capital of Anuradhapura District.

but I want display some words only like this

Anuradhapura major city Sri Lanka.capital city North Central Province, Sri Lanka capital Anuradhapura District.

This this how now I display my description

Review.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGrid();
    }
}

private void BindGrid()
{
    string constr = "Data Source=(localdb)\\ProjectsV13;Initial Catalog=ReviewDB;Integrated Security=True";
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT Description, Place FROM Reviews"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}

Review.aspx

    <body>

    <form id="form1" runat="server">
        <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
    OnPageIndexChanging="OnPaging" PageSize="10">
    <Columns>
        <asp:BoundField ItemStyle-Width="500px" DataField="Description" HeaderText="Description" />

    </Columns>
</asp:GridView>
    </div>

    </form>
</body>
Developer
  • 145
  • 2
  • 5
  • 20

3 Answers3

0

This should be simple if your words are separated by space only:

var DescWords = Description.Split(" ".ToCharArray());

//WordsToRemove is the list of words that you don't want to show.
var NewDesc = String.Join(" ", DescWords.Except(WordsToRemove));

Make sure you add using System.Linq; at the top of the file. Also note that you need to replace Description with the name of the actual DataTable value that you want to fix. For example, this could be dt[0]["Description"] or something.

If your description contains other punctuations, this will become a bit more involved, but can still be achieved pretty easily.

Edit

If your string contains multiple delimiters, you can handle it like this:

var WordsToRemove = new[] { "a", "is", "about", "above", "after", "again", "against", "all" };

var Delimiters = ".,;: ()!?/";
int LastIndex = 0;
var NewDesc = "";

for (int row = 0; row < dt.Rows.Count; row++)
{
  var Description = dt.Rows[row]["Description"].ToString();
for (int i = 0; i < Description.Length; i++)
{
  if (Delimiters.IndexOf(Description[i]) >= 0)
  {
    var Word = Description.Substring(LastIndex, i - LastIndex + 1);
    if (WordsToRemove.Contains(Word.Trim(Delimiters.ToCharArray())))
      NewDesc += Description[i];
    else
      NewDesc += Word;

    LastIndex = i + 1;
  }
}
}

Inject this code below sda.Fill(dt); line in your code. In real world, problems like this are solved slightly differently, e.g. you should consider moving this code to a function, plus you should use a StringBuilder instead of string, but since you're a beginner, I'm just leaving hints for you.

dotNET
  • 33,414
  • 24
  • 162
  • 251
0

Access the content of DataTable as such:

string myData=dt.Rows[0][1].ToString(); 

Create a loop for the loop for the string to remove the desired words

string bannedWords = @"\b(this|is|the|list|of|banned|words)\b";

foreach(data in myData)
    string replaced = Regex.Replace(data, bannedWords, "", RegexOptions.IgnoreCase);

Then plug the string in the GridView

  • Two things: Regex is a bit of an overkill for this situation. Second and more important, this will leave double spaces everywhere in the sentence. – dotNET Jun 10 '16 at 09:36
  • @Alfie McNarutoad shall i try your idea? and how to display in a view my first c# program this :( – Developer Jun 10 '16 at 10:04
0

This is the snippet for removing unwanted words in a string

string description = "Anuradhapura is a major city in Sri Lanka. It is the capital city of North Central Province, Sri Lanka and the capital of Anuradhapura District";
List<string> ignoredWords = new List<string>() { "a","is","about","above","after","again","against","all", "just", "text","of","and","it","the" };
description = string.Join(" ", description.Split().Where(words => !ignoredWords.Contains(words, StringComparer.InvariantCultureIgnoreCase)));

OUTPUT

Anuradhapura major city in Sri Lanka. capital city North Central Province, Sri Lanka capital Anuradhapura District

Now, as you requirement I have merged the code to give a complete answer.

Here I have created a sample DataTable, whereas you have it from Databaseand I have removed the paging, do add it in your code.

ASPX

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPaging" PageSize="10" OnRowDataBound="GridView1_RowDataBound">
  <Columns>
  <asp:BoundField ItemStyle-Width="500px" DataField="Description" HeaderText="Description" />
</Columns>

Here I have added OnRowDataBound event to it.

CS

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGrid();
    }
}

private void BindGrid()
{
    string constr = "Data Source=(localdb)\\ProjectsV13;Initial Catalog=ReviewDB;Integrated Security=True";
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT Description, Place FROM Reviews"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}


//In `RowDataBound` I'm doing all the manipulations
   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView drv = (DataRowView)e.Row.DataItem;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (drv["Description"] != DBNull.Value)
            {
                string description = Convert.ToString(drv["Description"]);
                e.Row.Cells[0].Text = RemoveUnwanedWords(description);
            }
        } 
    }

    public string RemoveUnwanedWords(string description)
    {
        List<string> ignoredWords = new List<string>() { "a", "is", "about", "above", "after", "again", "against", "all", "just", "text", "of", "and", "it", "the" };
        return string.Join(" ", description.Split().Where(words => !ignoredWords.Contains(words, StringComparer.InvariantCultureIgnoreCase)));
    }
Chaitanya
  • 171
  • 1
  • 7
  • @Developer Let me see your `Gridview` control, in the aspx page – Chaitanya Jun 10 '16 at 10:09
  • please chek my question @durga – Developer Jun 10 '16 at 14:32
  • if you can merge your idea with my code plz and let me know how to pass aspx page also – Developer Jun 10 '16 at 14:51
  • @Developer Do see my updated answer and let me know the status – Chaitanya Jun 10 '16 at 19:21
  • check this you calling `GetData();` so it will return same ` _reviewer["Description"] = "Anuradhapura is a major city in Sri Lanka. It is the capital city of North Central Province, Sri Lanka and the capital of Anuradhapura District"; _reviewer["Place"] = "Country";` ????? – Developer Jun 10 '16 at 21:50
  • @Developer Why are you are worried about the static data,You have a query of `SELECT Description, Place FROM Reviews`. So I have taken two columns of `Description` and `Place`. I have clearly mentioned that you have to get it from a `database`. You just have to use `this.BindGrid();` in the `!this.IsPostBack` condition of `Page_Load` method – Chaitanya Jun 11 '16 at 01:49
  • so between where i want paste to ur code? im little confuse ? – Developer Jun 11 '16 at 04:53
  • if i use `this.BindGrid();` it will call my methods right not your any methods sry if im rong ? – Developer Jun 11 '16 at 06:04
  • @Developer See I have updated check it and let me know – Chaitanya Jun 11 '16 at 06:37
  • Hello whts im wrong or you with your coding chek when i call page_load its calling my function right `this.BindGrid();` thr is no any connection with ur functions right so im getting same out put which i got from my code :) – Developer Jun 11 '16 at 07:13
  • can you check please – Developer Jun 11 '16 at 07:18
  • Omg sorry mam :) its working – Developer Jun 11 '16 at 07:42
  • wheni get 15 ponits only it will automatically go up :) – Developer Jun 12 '16 at 07:54