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 am using a datagridview to show data in csv file. One of the column in datagridview in of numeric type [Column name: ID].
I am using autosort method of datagridview(sorting by clicking column header).
This works well for all the columns except this numeric column.
This column contains numbers 1 to 55
What I am getting now is after sorting is:
1,10,11,12,13,14,15,16,17,18,19,2,20,21,22,23,24,25,26,27,28,29,3,30,31...
...and so on. what I want is:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,...
Please help.
Thanks in advance.
You can use the event SortCompare like this:
private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e) {
//Suppose your interested column has index 1
if (e.Column.Index == 1){
e.SortResult = int.Parse(e.CellValue1.ToString()).CompareTo(int.Parse(e.CellValue2.ToString()));
e.Handled = true;//pass by the default sorting
}
}
Build your DataTable:
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(Int32));
dt.Columns.Add("Name");
dt.Columns.Add("ForeName");
I am using these columns due to the fact that I don't know what data you are storing.
After reading your csv-file you have to iterate through the data. Considering you have your data stored in a String array (String[][] arr) with the inner ArrayLength of 3 (ID, Name, ForeName).
for(int i=0;i<arr.Length;i++)
{
DataRow row = dt.NewRow();
row["ID"] = Convert.ToInt32(arr[i][0]);
row["Name"] = arr[i][1];
row["ForeName"] = arr[i][2];
dt.Rows.Add(row);
}
DataGridView dgv = new DataGridView();
dgv.DataSource = dt;
I hope this helps, it's a sample Code.
The reference to DataTable: https://msdn.microsoft.com/de-de/library/system.data.datatable(v=vs.110).aspx
EDIT
I just read that you already have a DataTable as result. You can build another DataTable as I did and iterate through your Data and convert the ID to an Integer. Please don't forget to set the Columns Type:
dt.Columns.Add("ID", typeof(Int32));
Well, your column is not numeric, but a text column that contains numeric string values. There's a huge difference! A real numeric column would apply numeric sorting, while your column uses alphanumeric sorting.
I'd try to actually change the underlying data type to int instead of string or apply manual sorting as suggested by lem2802.
Please add some information about how you fill the data grid view. Maybe that can help find an easier way than implementing a manual sort comparison.
According to your comment you create a DataTable as the data source for the grid view. You need to make sure that the column(s) containing numeric values also have a numeric type. In the code where you create the table and its columns, do something like this:
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(Int32));
... // Other columns
Later when you create the rows based on the content of your CSV file, make sure to fill each row so that the value for the ID column actually is an int:
DataRow row = table.NewRow();
row["ID"] = Convert.ToInt32(idValueFromCSV);
... // Other columns
That way you'll get what you want without implementing custom sorting.
for example:
table1 has two columns "col1" and "col2"
this.table1TableAdapter.FillByParam(appDataSet.table1,param);
this returns 1 row
how to set col1 and col2 values to variable?
It would be great if someone post example with code.
Thanks.
This line fills a given DataTable from the query that is specififed as SelectCommand in the TableAdapter. So you say that it just contains a single record?
You can use FirstOrDefault to get that DataRow. Since it's a strongly typed DataTable you have an autogenerated DataTable and DataRow with typed properties and names that are the same as the ctypes and names in database.
So assuming you have two Label controls which Text properties you want to set from the two properties in the row:
appDataSet.table1Row row = appDataSet.table1.FirstOrDefault();
if(row != null)
{
Label1.Text = row.col1;
Label2.Text = row.col2;
}
I have DataTable object and am binding it to a gridview in C#.
I have 3 columns in the datatable, say "Flag", "Name", and "Value".
What I want to accomplish is that I want to only show the rows where "flag" fields are set to 0.
So say if I have two rows in the table,
Flag Name Value
------------------
0 tom 100
1 Jane 200
And, I only want to show "tom" and "100" on the gridview.
Is there any way I could do this without creating a new datatable?
Thanks.
Here is an example :
DataTable table = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string expression;
expression = "Date > #1/1/00#";
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]);
}
You can see example Here
Probably you can take the same Datatable like : table = table.Select(...);
try creating a DataView for your DataTable and send it to your GridView instead of the DataTable. see http://msdn.microsoft.com/en-us/library/system.data.dataview.aspx
I have two DataTables.
First is
DataTable NameAddressPhones = new DataTable();
with Three columns Name, Address and PhoneNo.But I only want two columns Name and Address data so I want to copy those columns (with data) to the new DataTable.
DataTable NameAddress = new DataTable();
For that I do
foreach (DataRow sourcerow in NameAddressPhones.Rows)
{
DataRow destRow = NameAddress.NewRow();
foreach (string colname in columns)
{
destRow[colname] = sourcerow[colname];
}
NameAddress.Rows.Add(destRow);
}
I clear the NameAddressPhones(first) DataTable every time there are new records inserted in the table. And every time there will be the same number of columns but the column names will be different like Nm instead of Name, Add instead of Address.Now the problem is the second DataTable already has column names Name and Address and now I want to copy the columns data of Nm and Add to the second DataTable but the column names are different than the column names of the second DataTable. So even if there are different column names I want to copy Nm column data of first DataTable to the column Name of second DataTable and column Add data of first DataTable to column Address of second DataTable.
In short how can we copy column data from one DataTable to another even if there are different column names of both DataTables like Nm is the column name of first DataTable and Name is the column name of second DataTable then the data of the column Nm should be copied to the column Name.
Here's the simplest way:
foreach (DataRow sourcerow in NameAdressPhones.Rows)
{
DataRow destRow = NameAdress.NewRow();
destRow["Name"] = sourcerow["Nm"];
destRow["Address"] = sourcerow["Add"];
NameAdress.Rows.Add(destRow);
}
Automation is great when it's available. When it's not, you have to map source columns to destination columns in some manner.
If the columns are in the same order in both tables, you could just reference the values by ordinal instead of column name, but that's such a bad idea I'm not even going to post any code for it.
Use column index number rather than names:
destRow[0] = sourcerow[0]; // for column 0 = "Name" or "NM"
If I've understood your question right, then the way this is usually done is by using stored procedures. You have the same stored procedures in both databases, but the implementation is specific to the table schema of each database. This allows you the abstraction you need.