C# Datatable query - c#

I want to search two string in datarows.For example;
string1="ex"
string2="ex2"
row1={'ex','ex2','ex3'....}---->True
row2={'ex3','ex1','ex2'....}---->True
row3={'ex2','ex5','ex6'....}---->False
Each line must have a value of two strings..
For This,
for (counter = 0; counter < array.Count; counter++)
{
int index=0;
ArrayList array3 = new ArrayList();
array3 = Split(array[counter].ToString());
foreach (DataRow row2 in data.Rows)
foreach (object obje in row2.ItemArray)
{
//Proceeds
}
}
}
I coding something.But I do not want to deal with pollution in the code...
Is there an easy way to select method?

var result = data.AsEnumerable()
.Where(r => r.ItemArray.Contains(string1) || r.ItemArray.Contains(string2))

Try to do something like this:
first define a DataView
DataView dv = new DataView(dt) where dt is a DataTable.
After apply RowFilter
dv.RowFilter = "CONTAINS(ColName, 'ex1') AND CONTAINS(ColName, 'ex2')".
Hope this helps.

Related

C# - Jagged string[][] arrayname into DataTable/DataGridView

I am new to C# programming and topic of operating with jagged array.
I have some data stored in my string[][] arrayname and want to show it in datagridview.
Will be very grateful if you could advice me on the case.
You need to create dataset, usually I use a DataTable, I have drafted a solution to your problem, but you have to using Linq:
var ListName = arrayname.ToList();
//get number of column, probalby you dont need it
int cols = ListName.Select(r => r.Length).Max();
//Create a datasource
DataTable dt = new DataTable();
//Write column, probalby you dont need it
for (int f = 0; f < cols; f++)
dt.Columns.Add("Col " + (f+1));
foreach (var row in ListName) {
//make a row
List<string> Lrow = new List<string>();
Lrow.AddRange(row);
//if row is too short add fields
if (Lrow.Count < cols)
for (int i = Lrow.Count; i < dt.Columns.Count; i++) {
Lrow.Add("");
}
//at last add row to dataTable
dt.Rows.Add(Lrow.ToArray());
}
//and set dataGridView's DataSource to DataTable
dataGridView1.DataSource = dt;
The result should be this

how do I make my DataGridView equal to my List<List<decimal>>?

I have a List of decimal lists, so when filled it's essentially a grid...
2.0, 3.1, 4.0
3.2, 7.0, 1.4
6.0, 3.1, 8.8
The code for the List...
List<List<decimal>> Rows = new List<List<decimal>>();
Is there an easy way to make me DataGridView display this data, I tried...
DataGridView1.DataSource = Rows;
But this did not work.
the grid outputs like this...
One way to do this is to convert the list of lists to a DataTable object and then set it as the DataSource property. Here is an example of a method that does such conversion:
public DataTable CreateDataTable(List<List<decimal>> data)
{
DataTable table = new DataTable();
if (data.Count == 0)
return table;
int fields = data[0].Count;
for (int i = 0; i < fields; i++)
{
table.Columns.Add("column" + (i + 1), typeof (decimal));
}
foreach (var list in data)
{
var row = table.NewRow();
for (int i = 0; i < fields; i++)
{
row.SetField(i, list[i]);
}
table.Rows.Add(row);
}
return table;
}
And then you can set the DataSource property like this:
DataGridView1.DataSource = CreateDataTable(Rows);
The reason you are seeing Capacity and Count is because these are the properties exposed by List<T>, and these properties are used by GridView for showing the values.
There are multiple ways of doing it, like using a DataTable or you can use a LINQ expression to specify column names like:
dataGridView1.DataSource = Rows.Select(d => new
{
Col1 = d[0],
Col2 = d[1],
Col3 = d[2]
}).ToList();
(You may add a check for column count).

How to declare datarow outside foreach loop?

I am using foreach loop and inside that loop I declared datarow
DataSet ds = Business.Site.GetSiteActiveModulesWithStatus(siteId);
foreach (DataRow dr in ds.Tables[0].Rows)
How can I implement this datarow outside foreach loop and how can I use for loop instead of foreach loop?
To loop in a for:
for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
//To acess the row
DataRow row = ds.Tables[0].Rows[i];
}
Outside the for, to acess a specific DataRow you can do like this:
//Change 0 to other numbers to acess other rows
DataRow row = ds.Tables[0].Rows[0];
To access the row variable outside of the loop, you simple have to declare it outside:
DataRow rowFound = null;
for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
var currentRow = ds.Tables[0].Rows[i];
if(true /*To do: define some matching criteria*/)
{
rowFound = currentRow;
}
}
if(rowFound != null)
{
// We found some matching, what shall we do?
}
But you could write the same also in a LINQish style:
var rowFound = ds.Tables[0].AsEnumerable()
.Where(row => true /*To do: define some matching criteria*/)
.FirstOrDefault();
All code in this answer is untested.

how to clone/copy a datatable with only first n columns using linq

I have a datatable containing over 100 columns, how ever I need to strip out all columns
except first 11 columns.
I need to retain data of 1st 11 columns.
I am doing it with following code
public DataTable validdatatable(DataTable table)
{
DataTable dt = new DataTable();
for (int i = 0; i < 11; i++)
{
DataColumn dc = new DataColumn();
dc.ColumnName = table.Columns[i].ColumnName;
dc.DataType = table.Columns[i].DataType;
dt.Columns.Add(dc);
}
for (int i = 0; i < table.Rows.Count; i++)
{
object[] ob = table.Rows[i].ItemArray;
...
...
}
return dt;
}
This methods works but is too heavy on CPU and Ram.
Is there any other method with which I can proceed?
Try this:
public DataTable validdatatable(DataTable table)
{
var dt = table.Columns.Cast<DataColumn>().Take(11);
return dt.CopyToDataTable();
}
Or Something like this. It will give you at least a way to work on it.
Note that You need to add a reference to the assembly: System.Data.DataSetExtensions.dll then you can write your function like above.
You can try this. The only difference would be instead of object[] ob = table.Rows[i].ItemArray it will just grab the first 11 columns using the index and make an array out of that (itemArray will make an array of all 100 columns). Still doubt this will solve your memory issues if you are that tight but it's probably worth a shot.
var copyDt = new DataTable();
for (var i = 0; i < 11; i++)
{
copyDt.Columns.Add(dataTable.Columns[i].ColumnName, dataTable.Columns[1].DataType);
}
copyDt.BeginLoadData();
foreach (DataRow dr in dataTable.Rows)
{
copyDt.Rows.Add(Enumerable.Range(0, 11).Select(i => dr[i]).ToArray());
}
copyDt.EndLoadData();

How can I know a row index while iterating with foreach?

in the next example how can I know the current row index?
foreach (DataRow temprow in temptable.Rows)
{
//this.text = temprow.INDEX????
}
You have to create one yourself
var i = 0;
foreach (DataRow temprow in temptable.Rows)
{
this.text = i;
// etc
i++;
}
or you can just do a for loop instead.
I have a type in MiscUtil which can help with this - SmartEnumerable. It's a dumb name, but it works :) See the usage page for details, and if you're using C# 3 you can make it even simpler:
foreach (var item in temptable.Rows.AsSmartEnumerable())
{
int index = item.Index;
DataRow value = item.Value;
bool isFirst = item.IsFirst;
bool isLast = item.IsLast;
}
If you can use Linq, you can do it this way:
foreach (var pair in temptable.Rows.Cast<DataRow>()
.Select((r, i) => new {Row = r, Index = i}))
{
int index = pair.Index;
DataRow row = pair.Row;
}
You actually Don't. One of the beauties with foreach is that you don't have the extra set of code handling incrementing and checks on the length.
If you want to have your own Index you would have to do something like this
int rowindex = 0;
foreach (DataRow temprow in temptable.Rows)
{
//this.text = temprow.INDEX????
this.text = rowindex++;
}
int rowIndex = temptable.Rows.IndexOf(temprow);
It's not possible with a standard foreach loop. The simplest way is to use a for loop
for ( int i = 0; i < temptable.Rows.Count; i++ ) {
DataRow temprow = (DataRow)temptable.Rows[i];
...
}
Another option is to use an extension method
public static void ForEachIndex<T>(this IEnumerable<T> e, Action<T,int> del) {
var i = 0;
foreach ( var cur in e ) {
del(cur,i);
}
}
...
temptable.Rows.Cast<DataRow>.ForEachIndex((cur,index)
{
...
});
Hey there's a much faster way I think. No iteration required!
First, declare a static variable for the Friend RowID Field of the DataRow:
Private Shared RowIDFieldInfo As System.Reflection.FieldInfo = GetType(DataRow).GetField("_rowID", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance)
Then All you need to do to use it is:
RowIDFieldInfo.GetValue(MyDataRow) - 1
I have not tested this after resorting or filtering.
In my case I haven't a need to do that, so this works.
Better late than never...
foreach (DataRow temprow in temptable.Rows)
{
temptable.Rows.IndexOf(temprow);
}
Write any Cell number and get RowIndex
foreach (var item in datagridview.Rows)
{
//TextBox1.Text= item.Cells[0].RowIndex.ToString();
}
Either use a for-loop, or use an integer follow along:
int count =0;
foreach (DataRow temprow in temptable.Rows)
{
//count is the index of the row in the array temptable.Rows
//this.text = temprow.INDEX????
++count;
}
You can use the standard for loop to get the index
for(int i=0; i<temptable.Rows.Count; i++)
{
var index = i;
var row = temptable.Rows[i];
}
While LFSR's answer is right, I'm pretty sure calling .IndexOf on just about any collection/list is going to enumerate the list until it finds a the matching row. For large DataTable's this could be slow.
It might be better to for (i = 0; i < temptable.Rows.Count; i++) { ... } over the table. That way you have the index without imposing a find-the-index tax.
The alternative way to retrieve data by using index instead of using column name
foreach (DataRow temprow in temptable.Rows)
{
String col1 = temprow[0].ToString().Trim();
String col2 = temprow[1].ToString().Trim();
}
Hope it help

Categories

Resources