I'm trying to set selectively display certain text using a databind.
the code looks like this..
DataTable oDt;
oDt = Apps.GetAll();
if (oDt.Rows.Count > 0)
{
oDt.Columns.Add("AppName_ID", typeof(string), "App_Name + ' (' + App_ID + ')'");
CmbApps.DataSource = oDt;
CmbApps.DataValueField = "App_ID";
CmbApps.DataTextField = "AppName_ID";
CmbApps.DataBind();
}
The problem is that the first value shows up as: Select (0).. so I'm trying to change the datatextfield when "App_ID" = 0 so that App_ID is NOT displayed, but is in all other values.
Not sure about the syntax, but it will be close to the below.
DataTable dt2 = oDt.Select("(App_ID != 0)").CopyToDataTable();
Using Linq-To-DataTable
DataTable tblFiltered = oDt.AsEnumerable()
.Where(row => row.Field<String>("App_ID") == "1")
.CopyToDataTable();
Or you can use DataView and RowFilter
DataView dataView = oDt.DefaultView;
dataView.RowFilter = "App_ID <> 0";
UPDATE
foreach (DataRow DRow in oDt.Rows)
{
if(DRow["app_id"].ToString().Equals("0"))
DRow["AppName_ID"] = "Select";
}
Related
I need to sort a data table and load that data table values to a combo box.Then I need to add a new row called 'All' and it should appear at the top of the combo box items.Here is my work so far,
DataTable dt = bLStatus.GetStatusDetail();
if (dt != null && dt.Rows.Count > 0)
{
dt.DefaultView.Sort= "Description ASC";
cmbCurrentStatus.DataSource = dt;
cmbCurrentStatus.ValueMember = "Description";
cmbCurrentStatus.DisplayMember = "Description";
DataRow row = dt.NewRow();
row["Description"] = "All";
dt.Rows.InsertAt(row, 0);
}
Problem is Row 'All' gets sorted as well,I need to display the row at top How to solve this? Any help will be appreciated.
You could try to insert "All" into the combobox at 0th index. The syntax would be like:
Insert(int insertIndex, object insertItem);
An example would be:
cmbCurrentStatus.Items.Insert(0, (new myClass { Description = ""}));
Reference: https://social.msdn.microsoft.com/Forums/en-US/762a58f5-667d-4e97-a107-86312942f966/can-i-add-items-to-a-combobox-at-a-specific-index?forum=wpf
Similar post in SO: add an item to combobox before bind data from data base
Let me know if it doesn't work.
I answered my question I just needed to copy my Sorted defaultView to another new datatable and add row 'All' to that datatable and bind it to the combo box. Like below,
DataTable dt = bLStatus.GetStatusDetail();
if (dt != null && dt.Rows.Count > 0)
{
dt.DefaultView.Sort = "Description ASC";
DataTable dt2 = dt.DefaultView.ToTable();
DataRow row1 = dt2.NewRow();
row1["Description"] = "All";
dt2.Rows.InsertAt(row1, 0);
cmbCurrentStatus.DataSource = dt2;
cmbCurrentStatus.ValueMember = "Description";
cmbCurrentStatus.DisplayMember = "Description";
}
I have a combobox called cmbCaseRemain its data populated from datatable by code
cmbCaseRemain.DataSource = ce.GET_ALL_CASEREMAIN();
cmbCaseRemain.DisplayMember = "caseRemain";
cmbCaseRemain.ValueMember = "idCaseRemain";
and I have a datagridview called dgv_CaseRemain gets its data from another datatable
DataTable dt = new DataTable();
dt = ce.GET_ALL_CASEREMAIN_FILTER(Convert.ToInt32(txtidCase.Text));
dgv_CaseRemain.DataSource = dt;
I'm using the combobox to add items to the datagrid view ... and i want after every adding to filter the items in the combobox so the user can't add the same value twice ... so I created a stored procedure with a parameter
CREATE PROC [dbo].[FILTER_CMB_CASEREMAIN]
#ID int
AS
SELECT
[idCaseRemain], [caseRemain]
FROM
[dbo].[tblCaseRemain]
LEFT OUTER JOIN
tblCase_Link_Remain ON idCaseRemain = idCaseRemain_L
WHERE
[idCaseRemain] <> #ID;
and using a code to pass the parameter which filter the combobox when i click on it
DataTable dt = new DataTable();
dt = ce.FILTER_CMB_CASEREMAIN(Convert.ToInt32(this.dgv_CaseRemain.CurrentRow.Cells[1].Value));
if (dt.Rows.Count > 0)
{
cmbCaseRemain.DisplayMember = "caseRemain";
cmbCaseRemain.ValueMember = "idCaseRemain";
cmbCaseRemain.DataSource = dt;
}
but I got an error
Object reference not set to an instance of an object.
Thank you
( sorry for my bad english :-) )
ok. Got it. As I said, it was error on that line. Use below code. Below I add the validation to check the null value.
int n = 0;
DataTable dt;
if (this.dgv_CaseRemain.CurrentRow.Cells[1].Value != null)
{
if (int.TryParse(this.dgv_CaseRemain.CurrentRow.Cells[1].Value.ToString(), out n))
{
dt = ce.FILTER_CMB_CASEREMAIN(n);
}
}
if (dt!=null && dt.Rows.Count > 0)
{
cmbCaseRemain.DisplayMember = "caseRemain";
cmbCaseRemain.ValueMember = "idCaseRemain";
cmbCaseRemain.DataSource = dt;
}
I have the following DataTable records :-
I want to display the Rows for which HeaderPrintOrder Column don't have 0 as value. And in PDF Cell I have to print FieldName : Amount by iterating to the records with above given condition.
I am trying the below code, gives me error Cannot interpret token '!'. What is correct way to do this?
var datatable = new DataTable();
datatable.Load(reader);
DataRow[] HeadingFields = datatable.Select("HeaderPrintOrder != 0");
foreach (var item in HeadingFields)
{
cellHead = new PdfPCell(new Phrase(HeadingFields[item]["FieldName"].ToString() + " : " + HeadingFields[item]["Amount"].ToString(), fntTableFont));
cellHead.Colspan = 3;
MainTable.AddCell(cellHead);
}
With LINQ it's easy:
var filtered = datatable.AsEnumerable()
.Where(row => row.Field<int>("HeaderPrintOrder") != 0);
foreach(DataRow row in filtered)
{
// ....
}
With DataTable.Select you have to use <> instead of !=:
DataRow[] HeadingFields = datatable.Select("HeaderPrintOrder <> 0");
<> is supported whereas != is not. You can see that here:
http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression%28v=vs.110%29.aspx
The != operator is not supported by the RowFilter syntax.
Try:
DataRow[] HeadingFields = datatable.Select("NOT (HeaderPrintOrder = 0)");
Try with Linq:
var HeadingFields= from row in datatable .AsEnumerable()
where row.Field<int>("HeaderPrintOrder") <> (int)(0)
select row;
I have created and returned datatable, this table has 10 columns. Now i want to filter from this table based on some dynamic search parameters. How to do this? any idea would be timely help.
// This function will create and return the source table.
var DisplayTable = CreateQueryTable();
Here I want to do dynamic search like If col1=MyName and Col2=MyCity
ResultGrid.DataSource = DisplayTable;
ResultGrid.DataBind();
Panel1.Controls.Add(ResultGrid);
You can do this in these way,
1.Creating DataView Like
var dv = dataTable.DefaultView;
dv.RowFilter = "col1='MyName' and Col2='MyCity'"; // if MyName and MyCity are literal string.
or
dv.RowFilter = "col1='"+MyName+"' and Col2 ='"+ MyCity +"'";// if MyName and MyCity are string variable.
2.With DataTable Select Method, It will return array of DataRow
var rows = dataTable.Select("col1='MyName' and Col2='MyCity'"); //if string literal
or
var rows = dataTable.Select("col1='"+MyName+"' and Col2='"+MyCity+"'"); // if string variable
3.By Linq
var filterdData = from row in dataTable.AsEnumerable()
where row.Field<string>("col1") == "MyName"
&& row.Field<string>("col2") == "MyCity"
select row;
you create DataView of your datatable and use Filter
// Create a DataView
DataView dv = new DataView(yourDataTable);
dv.RowFilter = "col1='MyName' and Col2='MyCity'";
//Bind your grid with DataView
You can also use select method on your table
DataRow[] foundRows;
foundRows = yourDataTable.Select("col1='MyName' and Col2='MyCity'");
You can also use Linq To DataTable
var results = from myRow in yourDataTable.AsEnumerable()
where myRow.Field<string>("col1") == Myname &&
myRow.Field<string>("Col2") == MyCity
select myRow;
I have to perform the aggregate function on the DataTable like Datatable.Compute but compute return the object i want to perform the aggregate function on the datatable and get the datarow .
_summaryTable.Compute("min(FareAdult)", whereClause
+ "AirlineDisplayName='"
+ Convert.ToString(airline["AirlineDisplayName"])
+ "' and ( Stops=0) ");
but above code will only return the min(FareAdult) but i want to select the two column based on the above condition from datatable.
How can i do it through Linq
I have to select min(FareAdult) and the TotelPrice value of same row
use Select instead compute
_summaryTable.Select("FilterationExpression");
DataRow[] dr = _summaryTable.Select("min(FareAdult),AirlineDisplayName='" + Convert.ToString(airline["AirlineDisplayName"]) + "' and ( Stops=0) ");
Here is a LINQ method. This is pseudocode since I don't know the typing of your row, and I haven't been able to test it, but the idea is the same. Use LINQ to select the rows that match your criteria, order by the FareAdult and then select the first (minimum).
var minResult = (from row in _summaryTable.Rows
where row.AirlineDisplayName == airline["AirlineDisplayName"] && row.Stops == 0
orderby row.FareAdult
select row).FirstOrDefault();
private void CalcColumns()
{
DataTable table = new DataTable ();
//enter code here
// Create the first column.
DataColumn priceColumn = new DataColumn();
priceColumn.DataType = System.Type.GetType("System.Decimal");
priceColumn.ColumnName = "price";
priceColumn.DefaultValue = 50;
// Create the second, calculated, column.
DataColumn taxColumn = new DataColumn();
taxColumn.DataType = System.Type.GetType("System.Decimal");
taxColumn.ColumnName = "tax";
taxColumn.Expression = "price * 0.0862";
// Create third column.
DataColumn totalColumn = new DataColumn();
totalColumn.DataType = System.Type.GetType("System.Decimal");
totalColumn.ColumnName = "total";
totalColumn.Expression = "price + tax";
// Add columns to DataTable.
table.Columns.Add(priceColumn);
table.Columns.Add(taxColumn);
table.Columns.Add(totalColumn);
DataRow row = table.NewRow();
table.Rows.Add(row);
DataView view = new DataView(table);
dataGrid1.DataSource = view;
}