I have a datatable with one autoincrement column. And I want to remove the row which contain null or white space values in all columns except autoincrement column.
var dr = TempRowToSelectionDT.Rows.Cast<DataRow>().Where(row => !row.ItemArray.All(field => field is System.DBNull || string.Compare((field as string).Trim(), string.Empty) == 0)).ToArray();
TempRowToSelectionDT is my datatable. I cannot remove the null rows in that datatable. Because I cannot filter column in itemarray and autoincrement column always has values. So I want to remove the rows in that datatable which contains null values in all cells except autoincrement column
Try this.
Dim dtSociete As New DataTable
dtSociete.Columns.Add("EmpID")
dtSociete.Columns.Add("EmpFName")
dtSociete.Columns.Add("EmpLName")
dtSociete.Rows.Add("1", "Faraz", "Ahmed")
dtSociete.Rows.Add("2", "Saad", "Aslam")
dtSociete.Rows.Add("3", "", "")
dtSociete.Rows.Add("4", "", "")
dtSociete = dtSociete.AsEnumerable().Where(Function(r) r.Field(Of String)("EmpName") <> "" And r.Field(Of String)("Descrip") <> "").CopyToDataTable()
Related
I have a DataTable with the Column name "Part Number" and various other columns. I want to grab all of the elements that are in the column "Part Number". This column can sometimes be in a different position so I can't just assign a particular Item Array index. I want to use LINQ to do it.
Right now, I just grab everything in the first column. However, I want to set this to grab the data according to the column heading.
var parts = from row in dataTable.AsEnumerable()
where true != string.IsNullOrWhiteSpace((row.ItemArray[0] == DBNull.Value)
? string.Empty
: row.ItemArray[0].ToString())
select row.ItemArray[0];
You can index a DataColumnCollection by a DataColumn, like this:
// Find the DataColumn by column name
string columnName = "Part Number";
DataColumn partNumber = dataTable.Columns[columnName];
var parts = from row in dataTable.AsEnumerable()
where !string.IsNullOrWhiteSpace(row[partNumber].ToString())
select row[partNumber];
Let the DataTable worry about finding the index inside the ItemArray. All you have to know is the column name.
For more information, see the documentation.
I have a Datatable that consists of more than 1000 record and a gridview that contains multiple records.I want to check if values present in grid view column for ex:- "AID" exists in datatable's AID column values. If any value of the gridview AID exists in Datatable AID i want to set a bool flag to true if no value exists in Datatable then to false.I want to check all values at a time, not one by one using for loop etc.I have tried the folowing code
List<string> lstInvdiscItem = (from row in dtinv.AsEnumerable()
select Convert.ToString(row["AID"])).ToList();
foreach (DataGridViewRow row in GrdItDetail.Rows)
{
string invdiscitem = row.Cells["AID"].Value.ToString();
int result = dtinv.Rows.IndexOf(dtinv.AsEnumerable().Where(g => g.Field<String>(0) == invdiscitem).FirstOrDefault());
if (result >= 0)
{
DiscFlag = true;
}
}
But above code checks one value at a time.My requirement is to check all values at a time just as we do using "IN" clause in sqlserver.
How can I achieve this task using c#.Any ideas? Any suggestions?
I have a Datatable, and I need to add "id" or "index" column with auto value.
like a identity column in sql.
I have a function that return me the start index: GetID();
I need that the value of this column in the first row will be the value that GetID() return, the value in the second row will be GetID() + 1.... GetID() + 2 ...
I need to do this without loops .
I thought to use "Expression" when I create new column, but I dont know how to do this.
You can use AutoIncrement property of DataTable column:
var dt = new DataTable();
var col = dt.Columns.Add("ID", typeof (long));
col.AutoIncrement = true;
col.AutoIncrementSeed = GetID();
col.AutoIncrementStep = 1;
But it will work only for rows being added to your DataTable after adding this column.
If you adding this identity column to the DataTable with existed rows - this will not update existed rows, so in this case you're still need loop over your existed rows and update them manually.
I have a datatable with columns a,b,c,d,e.
When column b is null, I want to set its value to the value of column a and then set all the other columns for that particular row to an empty string.
Try this:
System.Data.DataRow r = dt.Rows[0];
if ( r["b"] == System.DbNull.Value ) //DbNull assumes the nulls are coming from DB. If they are set to null via your code then you can use good old null instead.
{
r["b"] = r["a"];
r["c"] = "";
r["d"] = "";
}
I am using a datatable created by program. In this datatable i want to insert values in some specified columns.
Initially I am inserting primary key values leaving remaining columns null, when I am querying datatable with recently inserted value in Primary column to update same row, I am facing error Missing operand after ID operator
Can any one tell me the exact issue.
I am trying following code:
dt.Rows.Add(1);
int insertedValue = 1;
DataRow[] dr = dt.Select("ID = '" + insertedValue.toString() + "'");
And the table structure after entring primary value is as follows.
ID Volumn1 Volumn2 volumn3
--------------------------------------
1
You can do this more cleanly with LINQ and make this a strongly typed operation.
Something like:
dt.Rows.Add(1);
int insertedValue = 1;
var result =
dt.AsEnumerable().Where( dr => dr.Field<int>( "ID" ) == insertedValue );
Working example:
DataTable dt = new DataTable();
dt.Columns.Add( "ID", typeof( int ) );
dt.Rows.Add( 1 );
var result = dt.AsEnumerable().Where( dr => dr.Field<int>( "ID" ) == 1 );
You can simply format the selection string as shown below:
DataRow[] dr = dt.Select(string.Format("ID ='{0}' ", insertedValue));
Feel free to let me know if this works for you.. Thanks
You do not need ' ' in your filter.
I think this should work:
DataRow[] dr = dt.Select("ID = " + insertedValue.toString());
By the way, reference System.Data.DataSetExtensions
If you are looking for a specific row and your datatable has a primary key you could use the Find method and target the primary key which would return just the row you want rather than an array:
DataRow foundRow = dt.Rows.Find([INSERT SEARCH PARAMETER HERE]);
if(foundRow != null)
{
TO SET A STRING EQUAL TO A FOUND VALUE:
string str = foundRow["COLUMN NAME / INDEX];
OR IF YOU ARE INSERTING A VALUE YOU CAN USE IT LIKE THIS:
foundRow["COLUMN NAME / INDEX"] = NEW VALUE;
}
select column of row
dt.Rows[0].Field<string>("MyColumnName")