Filtering a empty string in DataTable - c#

How do filter a empty String in DataTable?
I need to filter a column (say Customer Name where the name is string.Empty)
I tried this but i cant get into right way..
I need to filter the DataView through DataView.RowFilter.. so how to give filter string for string.Empty..
Any idea on this?

To filter a dataTable-
dt.Select("customer_name = ''");
To Filter datatview-
dv.RowFilter = "customer_name = ''";

Use Select method:
DataRow[] foundRows = dt.Select("MyColumn = ''");

You can use Select method for DataTable:
//selects all customers which name is empty
var rows = dtData.Select("CustomerName = ''");

See the code below, might be a help. I am answering as the question has a tag RowFilters
private void GetRowsByFilter()
{
DataTable table = DataSet1.Tables["YourTable"];
// Presuming the DataTable has a column named Date.
string expression = "Column_name = ''";
// Sort descending by column named CompanyName.
string sortOrder = "ColumnName DESC";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression, sortOrder);
// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
Console.WriteLine(foundRows[i][0]);
}
}

Try below code:
DataTable dt=new DataTable();
DataRow dr;
dr=dt.NewRow();
if(dr["CustomerName"]==null)
{
put some code here.........
}
i Hope This code will help 4 u

Related

Searching for a string a column in datatable

I am getting a datatable object of a certain type from strongly typed dataset, now I want to find out if one of the column "Title" has certain string in it.
I am trying this, is there any better way ?
FruitDataAccess fda = new FruitDataAccess();
FruitDataTable fdt = cda.GetFriuts(fruitCrateID);
DataTable dt = fdt.CopyToDataTable();
var row = dt.Rows.Cast<DataRow>().Any(r => r.ItemArray.Any(c => c.ToString().Contains("whatever")));
Use LINQ TO DataSet/DataTable like:
var search = dt.AsEnumerable()
.FirstOrDefault(r=> r.Field<string>("Title") == "your string");
if(search != null)
{
//found
}
You can also find rows with your condition like:
DataRow[] foundRows;
foundRows = dt.Select("Title Like '%your string%'"); //similar to Contains
See: How to: Locate a Specific Row in a DataTable

Selecting a row from a particular column datatable without LINQ

My DataTable has the columns - Id, Name, Address. I need to select the column Address only WHERE ID = 7. How do I do this ? No LINQ please.
I was thinking of this -
DataView view = new DataView(MyDataTable);
DataTable distinctValues = view.ToTable(true, "ColumnA");
Now you can select.
DataRow[] myRows = distinctValues.Select();
//Get the desired answer by iterating myRows.
Is there a simpler way ?
thanks.
Well if you don't want to use LINQ, you can use a simple foreach loop:
DataTable distinctValues = view.ToTable(true, "ColumnA");
var myRows = new List<DataRow>();
foreach(DataRow row in distinctValues.Rows)
{
if(row["Id"].ToString() == "7") myRows.Add(row);
}

use select statement to get Data from a DataTable

I have DataTable containing three columns, Name, Date and DialedNumber. I want to get rows on the basis of DialedNumber column having phone number like 03001234567 ...
I am filing datatable with an method return type is datatable.
{
DataTable dt = filldata();
}
Problem is how to use select statement to get rows having number 03001234567 or some other telephone number ?
Try this Suppose you have a variable **string str** which is having that telephone number which you want to get from that data table then you can use this
{
DataTable dt = filldata();
DataRow[] resut = dt.Select("DialedNumber ='" + str + "'");
}
It will return you those rows having same telephone number in column DialedNumber.
If you want to filter from the start, not getting all table rows every time, you should adjust your SQL statement:
SELECT * FROM Table WHERE DialedNumber = #dialedNumber
and in C# use SqlCommand.Parameters.AddWithValue(...) to add the #dialedNumber parameter to the query.
Try to use Linq to DataTable like this
var results = from myRow in dt.AsEnumerable()
where myRow.Field<String>("DialedNumber") == "03001234567"
select myRow;
You can use Linq to DataSet:
string number = "03001234567";
var rows = dt.AsEnumerable()
.Where(r => r.Field<string>("DialedNumber").Contains(number));
You even can project rows into strongly typed objects:
var people = from r in dt.AsEnumerable()
where r.Field<string>("DialedNumber").Contains(number)
select new {
Name = r.Field<string>("Name"),
Date = r.Field<DateTime>("Date"),
DialedNumber = r.Field<string>("DialedNumber")
};
Note: if you want to check exact match of dialed number, then instead of Contains(number) (which is equivalent of LIKE) use == number.
Try like this
private void GetRowsByFilter()
{
DataTable table = DataSet1.Tables["Table1"];
// Presuming the DataTable has a column named Date.
string expression;
expression = "DialedNumber ='03001234567 '";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression);
// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
Console.WriteLine(foundRows[i][0]);
}
}
DataTable.Select Method

How to copy all the rows in a datatable to a datarow array?

I have two tables:
tbl_ClassFac:
ClassFacNo (Primary Key)
,FacultyID
,ClassID
tbl_EmpClassFac:
EmpID, (Primary Key)
DateImplement, (Primary Key)
ClassFacNo
I want to know all the Employees who are on a specific ClassFacNo. ie. All EmpID with a specific ClassFacNo... What I do is that I first search tbl_EmpClassFac with the EmpID supplied by the user. I store these datarows. Then use the ClassFacNo from these datarows to search through tbl_ClassFac.
The following is my code.
empRowsCF = ClassFacDS.Tables["EmpClassFac"].Select("EmpID='" + txt_SearchValueCF.Text + "'");
int maxempRowsCF = empRowsCF.Length;
if (maxempRowsCF > 0)
{
foundempDT = ClassFacDS.Tables["ClassFac"].Clone();
foreach (DataRow dRow in empRowsCF)
{
returnedRowsCF = ClassFacDS.Tables["ClassFac"].Select("ClassFacNo='" + dRow[2].ToString() + "'");
foundempDT.ImportRow(returnedRowsCF[0]);
}
}
dataGrid_CF.DataSource = null;
dataGrid_CF.DataSource = foundempDT.DefaultView;
***returnedRowsCF = foundempDT.Rows;*** // so NavigateRecordsCF can be used
NavigateRecordsCF("F"); // function to display data in textboxes (no importance here)
I know the code is not very good but that is all I can think of. If anyone has any suggestions please please tell me. If not tell me how do I copy all the Rows in a datatable to a datarow array ???
"How to copy all the rows in a datatable to a datarow array?"
If that helps, use the overload of Select without a parameter
DataRow[] rows = table.Select();
DataTable.Select()
Gets an array of all DataRow objects.
According to the rest of your question: it's actually not clear what's the question.
But i assume you want to filter the first table by a value of a field in the second(related) table. You can use this concise Linq-To-DataSet query:
var rows = from cfrow in tbl_ClassFac.AsEnumerable()
join ecfRow in tbl_EmpClassFac.AsEnumerable()
on cfrow.Field<int>("ClassFacNo") equals ecfRow.Field<int>("ClassFacNo")
where ecfRow.Field<int>("EmpId") == EmpId
select cfrow;
// if you want a new DataTable from the filtered tbl_ClassFac-DataRows:
var tblResult = rows.CopyToDataTable();
Note that you can get an exception at CopyToDataTable if the sequence of datarows is empty, so the filter didn't return any rows. You can avoid it in this way:
var tblResult = rows.Any() ? rows.CopyToDataTable() : tbl_ClassFac.Clone(); // empty table with same columns as source table

How to return a dataset from a dataset filtering value

I want to return a dataset filter from a static dataset.
Is it possible.?
You can filter Rows, by DataTable.Select function
private void GetRowsByFilter(){
DataTable myTable;
myTable = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string strExpr;
strExpr = "Date > '1/1/00'";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = myTable.Select(strExpr);
// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++){
Console.WriteLine(foundRows[i][0]);
}
}
Also You can get filtered DataSet by setting RowFilter property like this
ds.Tables[<table name>].DefaultView.RowFilter = "ProductId=5"
Look here for other ways to do filtering
But all of this methods does not create new DataSet with filtered data, if you need it , you should copy filtered rows manually I guess...

Categories