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
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.
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 a xtragrid on my form (let's say myXtraGrid)
And I bind a datatable:
myXtraGrid.DataSource = dtMyDataTable;
myXtraGrid.BestFitColumns();
Let's say I have just one column in datatable named "My Column" and it has three rows of data as below:
My Column
------
3
1
2
Than on grid (on runtime), I click on column header (My Column) and sort the data
(3 1 2) ascending: (1 2 3)
My question is; how can I get the sorted data as a Datatable as below?
My Column
------
1
2
3
On runtime I must get the sorted data and process it.
Thanks friends.
You can use DefaultView of Datatable and sort the view and later can be convert to table
if (dtMyDataTable.Rows.Count > 0)
{
DataView dv = dtMyDataTable.DefaultView;
dv.Sort = "cy_column ASC";
dtMyDataTable= dv.ToTable();
}
you can make a DataView.
myDataView dvMyView = new DataView(myDataTable);
This DataView can be sorted with:
dvMyView.Sort = " MyColumn ASC ";
So you have an sorted DataView which you can easily transform to a DataTable again:
myDataTable = dvMyView.toTable();
Now you have a sorted DataSource :-)
P.S In my opinion the BestPractice is to create an objectlist (List<myClass>). This Objectlist can be sorted. And an further advantage is that values changed in the Grid directly change your object in the List!
I hope that was helpful.
I have a labels that need values retreieved from a Database.
I am able to query the database but how can I extract values from a DataTable and place them in the appropiate labels
Thanks
In DataTable you have rows and columns. To select a particular cell you need to do this:
label1.Text = dataTable[0][0];
This will set the label1 text to Row 0, Column 0 value.
To iterate through each row use:
foreach(DataRow row in dataTable.Rows)
{
Console.WriteLine(row["ColumnName1"]);
Console.WriteLine(row["ColumnName2"]);
Console.WriteLine(row["ColumnName3"]);
Console.WriteLine(row["ColumnName4"]);
}
This will print values for columns against each row. In this code you need to replace string for columnname (e.g. ColumnName1) with your column names
Example of how you retrieve a value from the first row and the column named "MyFirstColumn":
label1.Text = myDataTable.Rows[0]["MyFirstColumn"]
How Create Data Table From Top 5 or 6 row in datagridview including column header also .
your explanation is too short. Any how, what i understand from this is that either you have a datable and you want to bind its just top 5 to 6 rows to data grid or u want to bring your data grid row to datatable with headers both can done through for each loop..
You can get column names like this from data
string columnName = mydatagrid.Columns[columnIndex].Name;
OR you can get both rows and column using foreach loop
foreach(DataGridColumn col in dg.Columns)
{
dt.columns.Add(col.HeaderText);
}
foreach(DataGridRow in dg.Rows)
{
if(dt.Rows.Count < 6)
//Add data to data table
}
The same way you can apply with little modifications to get data from datatable
You can use Linq as follow
datatable.AsEnumerable().Skip(5).Take(6);