datatable select filter where column timespan - c#

i want to ask, how do i set condition or expression where the column table is type of timespan.
when i use this code
private void button1_Click(object sender, EventArgs e)
{
string expression2;
expression2 = "timeOnlyStart < '" + TimeSpan.Parse("10:00:00") + "'";
DataTable yyy = dt_Main.Select(expression2).CopyToDataTable();
gridControl3.DataSource = yyy;
}
it gives me error .
EDITED : timeOnlyStart is a column start

You can use Linq to filter rows
var results = from myRow in dt_Main.AsEnumerable()
where myRow.Field<TimeSpan>("timeOnlyStart") < TimeSpan.Parse("10:00:00")
select myRow;
gridControl3.DataSource = results.AsDataView();
if you need datatable
var resultsdt = results.CopyToDataTable()

Related

Drawing several graphs from a listbox

I made a code like below. I would like there to be as many charts as are selected in the ListBox. Now they are always drawn all of them. For example, I would like to select listBox1 is one graph and e.g. listBox1, listBox2, listBox3 are 3 graphs.
How to change it or what to add?
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("value1");
listBox1.Items.Add("value2");
listBox1.Items.Add("value3");
listBox1.SelectionMode = SelectionMode.MultiSimple;
}
private void button1_Click_1(object sender, EventArgs e)
{
connect.Open();
string mysql = "SELECT value1, value2, value3, valuedate FROM mydata";
MySqlDataAdapter da = new MySqlDataAdapter(mysql, connect);
DataSet ds = new DataSet();
chart1.DataSource = ds.Tables[0];
chart1.Series["value1"].XValueMember = "valuedate";
chart1.Series["value1"].YValueMembers = "value1";
chart1.Series["value2"].XValueMember = "valuedate";
chart1.Series["value2"].YValueMembers = "value2";
chart1.Series["value3"].XValueMember = "valuedate";
chart1.Series["value3"].YValueMembers = "value3";
}
You should modify your query based on the items selected in the list view and then also similarly add the series.
The below code will give you an idea of how to approach it.
string query = "SELECT valuedate ";
for (int i = 0; i < listView1.SelectedItems.Count; i++)
{
string value = listView1.SelectedItems[i].Text;
query += ", " + value;
chart1.Series.Add(value);
chart1.Series[value].XValueMember = "valuedate";
chart1.Series[value].YValueMembers = value;
}
query += " FROM mydata";

Change FilterEditype of janus griex columns to checklistbox

i Have A Janus Gridex in Windows Form with Several Columns.
I want to Change the FilterEditType of All Columns in This GridEx to Checklistbox. So User Can Filter each Column Based on one or more than one values.
Please Give me the sample Code in C# or VB.net.
You need to add certain events for this one ,
First of all make the GridEx columns FilterEditType = Custom
Then add the Janus.Windows.GridEX.EditControls.CheckedComboBox for each column FilterEditType. For eg: add this events
private void grdTest_InitCustomEdit(object sender, Janus.Windows.GridEX.InitCustomEditEventArgs e)
{
if (e.Column.Key == "ColumnName")
{
cboTest.Visible = true;
e.EditControl = cboTest;
}
}
private void grdTest_EndCustomEdit(object sender, Janus.Windows.GridEX.EndCustomEditEventArgs e)
{
//this (dtTest) datatable is actually the datasource assigned for the grid
dtTest.DefaultView.RowFilter = string.Empty;
cboTest.Visible = false;
string Filter = string.Empty;
if (cboTest.CheckedItems != null)
{
foreach (GridEXRow row in cboTest.DropDownList.GetCheckedRows())
{
if (Filter != string.Empty)
Filter += " OR ColumnName = '";
else Filter = "( ColumnName = '";
Filter += row.Cells[2].Value;
Filter += "'";
}
Filter += " )";
}
dtTest.DefaultView.RowFilter = Filter;
grdTest.DataSource = dtTest.DefaultView;
}
private void grdTest_ClearFilterButtonClick(object sender, Janus.Windows.GridEX.ColumnActionEventArgs e)
{
if (e.Column.Key == "ColumnName")
cboTest.CheckAll();
grdTest_EndCustomEdit(null, null);
}
Then for the checked combo you need to design with multiple columns as you need, but one column should be a CheckBox and make it ActAsSelector = True,
The above example the checked combo has actually 3 columns , first one checkbox , second one is an ID (primary key ) and the last one is the name. I used the name column to filter the datatable.
In the DropDown event of the combo assign the datasource for it from the GirdEx datasource:
private void cboTest_DropDown(object sender, EventArgs e)
{
int C = 1;
DataRow DR;
DataTable dtComboDS= new DataTable();
dtComboDS.Columns.Add("ID", typeof(int));
dtComboDS.Columns.Add("Name", typeof(string));
var depts = (from x in dtTest.AsEnumerable() select x.Field<string>("ColumnName")).ToList();// dtTest is actually the datasource assigned for the GridEx
foreach (var Row in depts.Distinct().ToList())
{
DR = dtComboDS.NewRow();
DR["ID"] = C++;
DR["Name"] = Row;
dtComboDS.Rows.Add(DR);
}
cboTest.DropDownDataSource = dtComboDS;
}
private void cboTest_CloseUp(object sender, EventArgs e)
{
cboTest.Visible = false;
grdTest.UpdateData();
}
On the closeup event hide the checked combo and update the grid to get the result.
Hope this will help !
Good Luck.

How to show numbers in row of dataGridView

Hello my dear friends,
I have a DataGridView and made a database connection to get a table.
If the user clicks on the button he gets the information in DataGridView object.
It looks like this:
private void button3_Click(object sender, EventArgs e)
{
dataGridView1.Refresh();
dataGridView1.Columns.Clear();
clsMSSQL.clsMSSQL mssql = new clsMSSQL.clsMSSQL(2);
string sql = ("select CCase.RefNo AS Az, EventTemplate.EventCode AS Vorgang from ikaros.CCase join ikaros.Event on CCase.ID = Event.CCaseID join ikaros.EventTemplate on Event.EventTemplateID = EventTemplate.ID where EventTemplate.EventCode='IRVB' and Event.EventDate ='2014-07-03' order by CCase.RefNo ASC");
mssql.Query(sql);
mssql.Fetch();
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "Aktenzeichen";
dataGridView1.Columns[1].Name = "Vorgang";
while (!mssql.eof)
{
string[] arr_row = new string[2];
arr_row[0] = mssql.GetString("Az");
arr_row[1] = mssql.GetString("Vorgang");
dataGridView1.Rows.Add(arr_row);
mssql.Fetch();
}
dataGridView1.EndEdit();
dataGridView1.Refresh();
}
But I want to have numeric numbers on the left of the DataGridView.
So I tried:
row.HeaderCell.Value = String.Format("{0}", row.Index + 1);
But "row" and the "HeaderCell" class gives a error.

ASP.NET GridView Sort using DataTable.Select

Is it possible to sort an ASP.NET GridView using DataTable.Select("", sortExpression)?
I have a regular GridView with AllowSorting="true" and OnSorting="grdEmployees_Sorting".
protected void grdEmployees_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = getDataTable();
var sortExprOrder = e.SortDirection == SortDirection.Ascending ? " ASC" : " DESC";
var rows = dt.Select("", string.Format(e.SortExpression + "{0}", sortExprOrder));
grdEmployees.DataSource = rows;
grdEmployees.DataBind();
}
Not sure why, but this does not work. The grid shows a bunch of rows with three columns, RowError, RowState, and HasErrors (contains rows with all empty checkboxes).
Am I doing something wrong?
You don't need select for sorting DataTable.Select is for filtering
This how you sort
protected void grdEmployees_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = getDataTable();
var sortExprOrder = e.SortDirection == SortDirection.Ascending ? " ASC" : " DESC";
DataView dv = new DataView(dt);
dv.Sort = string.Format("{0} {1}",
e.SortExpression, sortExprOrder);
grdEmployees.DataSource = dv;
grdEmployees.DataBind();
}

linq query data not showing error

I am trying to show data from DataTable into GridView using a LINQ query but I am not able to understand that why this code is not working. Actually GridView shows RowError HasError message. I am totally confused.
Here's my code
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = (DataTable)Session["userTable"];
string compare = Request.QueryString["id"].ToString();
var data = from x in table.AsEnumerable()
where x.Field<string>("Userid") == compare
select x;
GridView1.DataSource = data;
GridView1.DataBind();
}
Your query returns IEnumerable<DataRow> to convert it to Datatble use CopyToDataTable() extension. MSDN
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = (DataTable)Session["userTable"];
string compare = Request.QueryString["id"].ToString();
IEnumerable<DataRow> data = from x in table.AsEnumerable()
where x.Field<string>("Userid") == compare
select x;
DataTable boundTable = data.CopyToDataTable<DataRow>();
GridView1.DataSource = boundTable;
GridView1.DataBind();
}
Try this,
var data = from x in table.AsEnumerable()
where x.Field<string>("UserId").ToUpper().ToString().Equals(compare.ToUpper().ToString())
select x;
DataTable boundTable = data.AsDataView().ToTable();
return boundTable;

Categories