i have built a datatable as in picture .. i shall inter sentence in a textbox such as: "the boy kick the ball " i need to check each word of that sentence if it does exist in that table so it will display the name of the column that contains the word in a listbox such as:(the => Article).
i did all the steps but i stops in the last step of checking and displaying the name of the column that contains the word..
i'm just a beginner so it becomes hard to me to programming the last steps would any one help me please???
datatable has three columns "Article","Noun","verb"
and rows values:"the","boy","kick" and so on in other rows
DataTable dt = new DataTable();
DataColumn article = new DataColumn("Article", typeof(string));
dt.Columns.Add(article);
DataColumn noun = new DataColumn("Noun", typeof(string));
dt.Columns.Add(noun);
DataColumn verb = new DataColumn("Verb", typeof(string));
dt.Columns.Add(verb);
DataRow dr = dt.NewRow();
dr["Article"] = "the";
dr["Noun"] = "boy";
dr["Verb"] = "kick";
dt.Rows.Add(dr);
dr = dt.NewRow();
DataRow dd = dt.NewRow();
dd["Article"] = "a";
dd["Noun"] = "ball";
dd["Verb"] = "eat";
dt.Rows.Add(dd);
dd = dt.NewRow();
dataGridView1.DataSource = dt;
string s = textBox1.Text;
string[] st = s.Split(' ');
now how to check each word in st[] if it does exist in the table
he will display the word and the name of the column he found it in
Hope it helps
using System;
using System.Data;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
DataColumn article = new DataColumn("Article", typeof(string));
dt.Columns.Add(article);
DataColumn noun = new DataColumn("Noun", typeof(string));
dt.Columns.Add(noun);
DataColumn verb = new DataColumn("Verb", typeof(string));
dt.Columns.Add(verb);
DataRow dr = dt.NewRow();
dr["Article"] = "the";
dr["Noun"] = "boy";
dr["Verb"] = "kick";
dt.Rows.Add(dr);
dr = dt.NewRow();
DataRow dd = dt.NewRow();
dd["Article"] = "a";
dd["Noun"] = "ball";
dd["Verb"] = "eat";
dt.Rows.Add(dd);
dd = dt.NewRow();
string s = "the boy eat the ball"; //textBox1.Text;
string[] st = s.Split(' ');
foreach (var str in st)
{
foreach (DataRow rows in dt.Rows)
{
if(str == rows["Article"].ToString())
Console.WriteLine("Word=" + str + " Column=Article");
else if (str == rows["Noun"].ToString())
Console.WriteLine("Word=" + str + " Column=Noun");
else if (str == rows["Verb"].ToString())
Console.WriteLine("Word=" + str + " Column=Verb");
}
}
}
}
}
Output:
Word=the Column=Article
Word=ball Column=Noun
Word=eat Column=Verb
Word=a Column=Article
Word=boy Column=Noun
Related
Captured all the required value in a Datatable.
Columns and corresponding values are added in a datatable.
**Need to pass the datatable value to the mail task in ssis package.
As it is in a table format** Please suggest
In mail values will be like this enter image description here
DataTable dt = new DataTable();
DataRow row;
DataColumn column;
column = new DataColumn();
dt.Columns.Add("Count_File_Date", typeof(String));
dt.Columns.Add("Count_File_Name", typeof(String));
dt.Columns.Add("Count_File_LMD", typeof(String));
dt.Columns.Add("Switch_File_Name", typeof(String));
dt.Columns.Add("Switch_File_Date", typeof(String));
dt.Columns.Add("Data_File_Name", typeof(String));
dt.Columns.Add("Data_File_LMD", typeof(String));
var directories = Directory.GetDirectories(directory);
foreach (string subdirectory in directories)
{
row = dt.NewRow();
dt.Rows.Add(row);
if (Directory.GetFiles(subdirectory, "*.count").Length == 0)
{
row["Count_File_Name"] = "Count File Not Found";
}
else
{
// Getting the values of count file
DateTime Count_L_M_D;
string Count_File_Name;
string[] Count_filePath1 = Directory.GetFiles(subdirectory, "*.count");
string Content = File.ReadAllText(#Count_filePath1[0]);
string loadedDate = DateTime.ParseExact(Content.Substring(9, 8), "yyyyMMdd",
CultureInfo.InvariantCulture).ToString("yyyy/MM/dd");
DateTime Datevalue = DateTime.Parse(loadedDate);
Count_File_Name = Path.GetFileName(Count_filePath1[0]);
Count_L_M_D = Directory.GetLastWriteTime(Count_filePath1[0]);
row["Count_File_Date"] = Datevalue;
row["Count_File_Name"] = Count_File_Name;
row["Count_File_LMD"] = Count_L_M_D;
}
if (Directory.GetFiles(subdirectory, "*.switch").Length == 0)
{
row["Switch_File_Name"] = "Switch File Not Found";
}
else
{
string[] Switch_filePath = Directory.GetFiles(subdirectory, "*.switch");
DateTime Switch_L_M_D;
string S_File_Name;
S_File_Name = Path.GetFileName(Switch_filePath[0]);
Switch_L_M_D = Directory.GetLastWriteTime(Switch_filePath[0]);
row["Switch_File_Name"] = S_File_Name;
row["Switch_File_Date"] = Switch_L_M_D;
}
}
I need to append rows one by one. The below code only adds the current rows and how can I add rows on top of the existing ones?
DataTable dt = new .DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Property", typeof(string));
DataRow row = dt.NewRow();
foreach (Object form in Obj)
{
string str1 = "hi";
string str2 = "hey";
row["Name"] = str1;
row["Property"] = str2;
dt.Rows.Add(row);
}
Really appreciate any suggestions?
Update 1:
My actual code
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Property", typeof(string));
if (crobj.SourceName.StartsWith("{#"))
{
foreach (CRObject crform in lstObj)
{
if (crform.FormulaForm == crobj.SourceName)
{
System.Data.DataRow newRow = dt.NewRow();
newRow["Name"] = "hi";
newRow["Property"] = "hey";
dt.Rows.Add(newRow);
}
}
}
You only have one row (DataRow) object which you keep re-editing. Declare a new one within the scope of the loop:
foreach (Object form in Obj)
{
DataRow row = dt.NewRow();
//...
dt.Rows.Add(row);
}
Small change:
DataTable dt = new .DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Property", typeof(string));
foreach (Object form in Obj)
{
DataRow row = dt.NewRow(); <<<------------
string str1 = "hi";
string str2 = "hey";
row["Name"] = str1;
row["Property"] = str2;
dt.Rows.Add(row);
}
By generating a new row inside the loop, it is treated as a new row instead of the same one.
EDIT: if this doesn't work, then the Obj collection is probably not containing values. Try this to test:
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Property", typeof(string));
for (int i=0; i<5; i++)
{
DataRow row = dt.NewRow();
string str1 = "hi";
string str2 = "hey";
row["Name"] = str1;
row["Property"] = str2;
dt.Rows.Add(row);
}
With this, the resulting data table contains 5 rows.
I`m trying to use something like this:
I have 20 list items and I have to choose some of them, then add them to datatable in first column elements, in second - their indexes.
DataTable dt = new DataTable();
dt.Columns.Add("Фактор-Критерій", typeof(string));
dt.Columns.Add("Ранг", typeof(string));
ChoosenCriteria.DataSource = dt;
List<string> selectedItems = new List<string>();
foreach (string o in listBox1.SelectedItems)
selectedItems.Add(o);
List<int> selectedItemIndexes = new List<int>();
foreach (int o in listBox1.SelectedIndices)
selectedItemIndexes.Add(listBox1.Items.IndexOf(o));
DataRow dr = dt.NewRow();
dr[0] = selectedItems.ToString();
dr[1] = selectedItemIndexes.ToString();
dt.Rows.Add(dr););
I can not do this, all i have at the end is on screen:enter image description here
Move the DataSource binding statement after you have done with the modification like
for(int i=0; i<selectedItems.Count;i++
{
DataRow dr = dt.NewRow();
dr[0] = selectedItems[i].ToString();
dr[1] = selectedItemIndexes[i].ToString();
dt.Rows.Add(dr);
}
ChoosenCriteria.DataSource = dt;
I am trying to add row in DataTable CartDT using row[], which is a string array.
DataTable CartDT = new DataTable();
public void DataTableColumn()
{
CartDT.Columns.Add("Product_Name", typeof(string));
CartDT.Columns.Add("Product_ID", typeof(string));
CartDT.Columns.Add("ItemQTY", typeof(string));
CartDT.Columns.Add("Price", typeof(string));
CartDT.Columns.Add("TotalPrice", typeof(string));
}
protected void AddToCart(object sender, GridViewCommandEventArgs e)
{
string[] arg = e.CommandArgument.ToString().Split(';');
int index = Convert.ToInt32(arg[3]);
TextBox itemQuantity = (TextBox)GridView1.Rows[index].Cells[6].FindControl("txtQty");
string[] row = new string[5];
row[0] = arg[0]; //Product_Name
row[1] = arg[1]; //Product_ID
row[2] = itemQuantity.Text; //OrderQTY
row[3] = arg[2]; //Price
row[4]=(Double.Parse(arg[2]) * Convert.ToInt32(itemQuantity.Text)).ToString();// calculate total price
CartDT.Rows.Add(row);//Creating row in Datatable using row[] string array
GridView2.DataSource = CartDT;
GridView2.DataBind();
}
Now when I execute it, it gives the error that "Input array is longer than the number of columns in this table"
The array row[] has exactly 5 elements in it & also DataTable CartDT has also 5 columns.
Now i am not able to find exactly where i am wrong.
Please help me to find the bug.
Thanx in advance.
Instead do this
DataRow dr = CartDT.NewRow();
Then
dr[0] = arg[0];
and so on. In the end
CartDT.Rows.Add(dr);
CartDT.AcceptChanges();
This way the instance of Row will have CartDT schema.
DataTable CartDT = new DataTable();
public void CreateDataTableColumns()
{
CartDT.Columns.Add("Product_Name", typeof(string));
CartDT.Columns.Add("Product_ID", typeof(string));
CartDT.Columns.Add("ItemQTY", typeof(string));
CartDT.Columns.Add("Price", typeof(string));
CartDT.Columns.Add("TotalPrice", typeof(string));
}
protected void AddToCart(object sender, GridViewCommandEventArgs e)
{
if (CartDT.Columns.Count = 0)
{
CreateDataTableColumns();
}
string[] arg = e.CommandArgument.ToString().Split(';');
int index = Convert.ToInt32(arg[3]);
TextBox itemQuantity =
(TextBox)GridView1.Rows[index].Cells[6].FindControl("txtQty");
DataRow dr = CartDT.NewRow();
dr[0] = arg[0]; //Product_Name
dr[1] = arg[1]; //Product_ID
dr[2] = itemQuantity.Text; //OrderQTY
dr[3] = arg[2]; //Price
dr[4] = (Double.Parse(arg[2]) * Convert.ToInt32(itemQuantity.Text)).ToString(); // calculate total price
CartDT.Rows.Add(dr);
CartDT.AcceptChanges();
GridView2.DataSource = CartDT;
GridView2.DataBind();
}
Debug, and break on
CartDT.Rows.Add(row);
and see how many columns are in CartDT; I think you don't call DataTableColumn() at the right time.
Your code having to do with row insertion works just fine
DataTable CartDT = new DataTable();
CartDT.Columns.Add("Product_Name", typeof(string));
CartDT.Columns.Add("Product_ID", typeof(string));
CartDT.Columns.Add("ItemQTY", typeof(string));
CartDT.Columns.Add("Price", typeof(string));
CartDT.Columns.Add("TotalPrice", typeof(string));
string[] row = new string[5];
row[0] = "1"; //Product_Name
row[1] = "2"; //Product_ID
row[2] = "3"; //OrderQTY
row[3] = "4"; //Price
row[4] = "5";// calculate total price
CartDT.Rows.Add(row);//Creating row in Datatable using row[] string array
for (int i = 0; i < 5; i++)
{
Console.WriteLine(CartDT.Rows[0][i]);
}
Console.Read();
Try Below Code:
protected void AddToCart(object sender, GridViewCommandEventArgs e)
{
string[] arg = e.CommandArgument.ToString().Split(';');
int index = Convert.ToInt32(arg[3]);
TextBox itemQuantity = (TextBox)GridView1.Rows[index].Cells[6].FindControl("txtQty");
DataRow row = CartDT.NewRow();
row["Product_Name"] = arg[0]; //Product_Name
row["Product_ID"] = arg[1]; //Product_ID
row["OrderQTY"] = itemQuantity.Text; //OrderQTY
row["Price"] = arg[2]; //Price
row["TotalPrice"]=(Double.Parse(arg[2]) * Convert.ToInt32(itemQuantity.Text)).ToString();
CartDt.Rows.Add(row);
CartDT.AcceptChanges();
GridView2.DataSource = CartDT;
GridView2.DataBind();
}
Following Code for transmission to the list box is :
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("BestSite", typeof(string));
dt.Columns.Add(dc);
for (int i = 1; i <= 10; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i.ToString() + " = stackoverflow";
dt.Rows.Add(dr);
}//EndFor
var Query = from mycolumn in dt.AsEnumerable()
where mycolumn.Field<string>("BestSite") != string.Empty
select mycolumn;
listBox1.DataSource = Query.AsDataView();
listBox1.DisplayMember = "BestSite";
Transfer to array what should be? no loop
string[] myvalue = new string[Query.AsDataView().Count];
Finally realized
Correct answer :
private string ConvertToString(DataRow dr)
{
return Convert.ToString(dr[0]);
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("BestSite", typeof(string));
dt.Columns.Add(dc);
for (int i = 1; i <= 10; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i.ToString() + " = stackoverflow";
dt.Rows.Add(dr);
}//EndFor
//var Query = from mycolumn in dt.AsEnumerable()
// where mycolumn.Field<string>("BestSite") != string.Empty
// select mycolumn;
DataRow[] myrow = new DataRow[dt.Rows.Count];
dt.Rows.CopyTo(myrow, 0);
string[] myString = Array.ConvertAll(myrow, new Converter<DataRow, string>(ConvertToString));
foreach (string a in myString)
{
listBox1.Items.Add(a);
}
}
If I understood your question correctly...
string[] myvalue = Query.Select(i => i.Field<string>("BestSite")).ToArray();
This example assumes:
=> datatable is non zero length
=> values are "parsable" to double.
coln = "PurchasePrice";
double[] arr_val = Array.ConvertAll<DataRow, double>
(
dattbl.Select(),
delegate (DataRow rw) { return
double.Parse(rw[coln].ToString()); }
);
// ... and for example
double total = arr_val.Sum();