How do I search data in from datatable based on user input - c#

I have created one datatable:
DataTable dt=new DataTable();
dt.Columns.Add("Country", typeof(string))
dt.Columns.Add("place", typeof(string))
dt.Columns.Add("Price", typeof(string))
dt.Columns.Add("Desc", typeof(string))
and I have inserted some data in this datatable:
DataRow dtrow = dt.NewRow(); // Create New Row
dtrow["Country"] = "India"; //Bind Data to Columns
dtrow["place"] = "wizag";
dtrow["Price"] = "7520";
dtrow["Desc"] = "Anywhere";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); // Create New Row
dtrow["Country"] = "India"; //Bind Data to Columns
dtrow["place"] = "Goa";
dtrow["price"] = "4500";
dtrow["Desc"] = "Anything";
dt.Rows.Add(dtrow);
I have bind this datatable with my grid view. I have also added one textbox and one search button in mmy aspx page. My requirement is that I want to search data based on user input e.g like 4500 or wizag, and if country name is the same I want to show it in one row
How can I do this?

I would use a DataView in this case
DataView dv = new DataView(dt);
dv.RowFilter = "price = 4500 AND Country = 'India'";
GridView.DataSource = dv.ToTable();
GridView.DataBind();

Related

GridControl showing no data but empty rows C#

I have a gridcontrol which needs to be populated with some data.
I have created a datatable and populated data in datarow and added to the table. Data is present in table on analysis. Datatable is then set as datasource for grid.
Code is below.
DataTable table = new DataTable();
table.Columns.Add("Tran Date");
table.Columns.Add("Due Date");
table.Columns.Add("Voucher");
table.Columns.Add("Vr No");
table.Columns.Add("Bill Amount");
table.Columns.Add("OutStanding");
table.Columns.Add("Remittance");
table.Columns.Add("Balance");
for (intcount=0;intcount<=BackupFunctions.mdtAgeingTemp.Rows.Count - 1;intcount++)
{
DataRow row = table.NewRow();
row["Tran Date"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][1];
row["Due Date"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][2];
row["Voucher"]= BackupFunctions.mdtAgeingTemp.Rows[intcount][3];
row["Vr No"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][4];
row["Bill Amount"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][10];
row["OutStanding"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][5];
row["Remittance"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][6];
row["Balance"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][7];
table.Rows.Add(row);
}
grdAgeing.DataSource = table;
Its found that 5 rows which is the result is displaying but they are empty. Could you figure out the issue in my code.

How to display specific row of data based on a column value

From this code I am trying to only display data rows that contain the fields "DBY" in the Station_From_Code column and "MAT" in the Station_To_Column. This will then be printed onto a HTML page. These fields are definitely in the datatable but when I run this cod the table is empty. I am not used to working with data tables in C# so apologies for the poor coding.
dynamic schedluedContent = JsonConvert.DeserializeObject(scheduledJson);
JArray items2 = new JArray();
foreach (JObject stops in schedluedContent.stops)
{
DataTable dt = new DataTable();
DataRow dr;
dr = dt.NewRow();
dr["From_Station_Code"] = stops["station_code"];
dr["To_Station_Code"] = stops["station_code"];
dt.Rows.Add(dr);
DataRow[] result = dt.Select("From_Station_Code = 'DBY' AND To_Station_Code = 'MAT'");
dt.Rows.Add(result);
GridViewTrainTimes.DataSource = dt;
GridViewTrainTimes.DataBind();
}
It's a long time I have not seen this style of code for working with database tables! EntityFramework comes to change and surely ease the way of working with database and prevent different risks of using SQL commands inside the code.
If you use EF, your code will become something like bellow:
var rows = myDBContext.MyTable.Where(x=>x.From_Station_Code == "DBY" && x.To_Station_Code == "MAT");
GridViewTrainTimes.DataSource = rows;
You can use find match rows and add in new datatable, that contains only your match rows data then you can bind this dt to gridview.
DataTable dt = new Datatable(); // Lets this datatable have data;
Datatable dt2 = new Datatable(); // Copy all matching rows
foreach (DataRow dr in dt.Rows)
{
if (dr["From_Station_Code"].ToString() == "DBY" && dr["To_Station_Code"].ToString() == "MAT")
{
dt2.Rows.Add(dr);
}
}
GridViewTrainTimes.DataSource = dt;
GridViewTrainTimes.DataBind();

How to reset sort for datagridview (DataTable or DataView)

DataTable table = new DataTable();
//DataView dv = table.DefaultView;
//dgvSurveyViewer.DataSource = dv;
dgvSurveyViewer.DataSource = table;
How to reset the sort after messing around the sort.
Initialize your data:
DataTable table = new DataTable();
...
Set the DataSource of grid to DefaultView of table instead of the table itself. Now you can set the sort field:
table.DefaultView.Sort = "name";
dgvSurveyViewer.DataSource = table.Defaultview;
....
Once you're finished with sorting, set the sort field to string.Empty:
table.DefaultView.Sort = string.Empty;
Grid will sense the change and will repaint the control.

How to add rows to GridView in asp.net c# application

I've already searched many articles on this page and couldn not find the answer that would fit me.
Ok i have two projects (one is server(host) and one is client). The first one is ASP.NET Empty Web Application and the second one is ASP.NET Web Forms site.
In my host i have code for displaying users from my Entity FrameWork database. And in my client i have a form (atleast im trying to create one) where i should be able to display data (users).
Code for displaying data works fine because i've already used it before.
But somehow i cannot create a code for Gridview, which would put data from database into my cells
Lets say ID column would display user ID...Name column would display user name...
I've already set up service reference.
This is a code i've written so far, but it's giving me a few errors.
GridView1.DataSource = null;
GridView1.Refresh();
ServiceReference1.mojWebServiceSoapClient client = new ServiceReference1.mojWebServiceSoapClient();
var userList = client.getUsers();
foreach (var user in userList)
{
DataTable dt = new DataTable();
DataColumn column = new DataColumn();
if (dt.Columns.Count == 0)
{
dt.Columns.Add("ID", typeof(Int32));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Last Name", typeof(string));
}
DataRow row = dt.NewRow();
row[0] = user.userID.ToString();
row[1] = user.Name;
row[2] = user.lName;
dt.Rows.Add(row);
GridView1.DataSource = dt;
GridView1.DataBind();
}
Can you guys please help me fix this? I would really appreciate any input.
Btw this code works, but it only displays 1 user, instead of all (5)
You repeat binding in a for loop and it is incorrect in your case. You should first create a DataTable and next in a for loop add users to it. So the following should satisfy your nedd:
GridView1.DataSource = null;
GridView1.Refresh();
ServiceReference1.mojWebServiceSoapClient client = new ServiceReference1.mojWebServiceSoapClient();
var userList = client.getUsers();
DataTable dt = new DataTable();
DataColumn column = new DataColumn();
dt.Columns.Add("ID", typeof(Int32));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Last Name", typeof(string));
foreach (var user in userList)
{
DataRow row = dt.NewRow();
row[0] = user.userID.ToString();
row[1] = user.Name;
row[2] = user.lName;
dt.Rows.Add(row);
}
GridView1.DataSource = dt;
GridView1.DataBind();
Hope that helped

Import data row to datatable using dataview

I want to insert a row from data view to data table. I am using the following code to insert dataview to datatable but it returns an error.
Unable to cast object of type 'System.Data.DataRowView' to type 'System.Data.DataRow'.
This is my partial code:
string empid= lbxempname.Items[i].Text.Substring(0,8);
DataView dv = new DataView();
dv = DS.DefaultView;
dv.RowFilter = "fldempid='" + empid + "'";
foreach (DataRow Dr in dv) {
DST.ImportRow(Dr);
}
A DataView contains DataRowViews and this has a property Row which references the DataRow.
foreach (DataRowView drv in dv)
{
DataRow dr = drv.Row;
}
But you cannot import that row into a DataTable if it belongs to another DataTable. I would use LINQ-To-DataSet to filter your rows and CopyToDataTable to create a new DataTable from it.
For example:
DataTable tblFiltered = DS.AsEnumerable()
.Where(r => r.Field<string>("fldempid") == empid)
.CopyToDataTable();
This might help?
foreach (DataRow row in dv)
{
dt.LoadDataRow(row.ItemArray);
}
Try this:
DST = dv.ToTable();
This line of code goes after you set filter for DataView.
Try this
DataView dview = new DataView();
DataTable dtable = new DataTable();
dtable = dview.Table.Clone();
foreach(DataRowView drowview in dview)
dtable.ImportRow(drowview.Row);

Categories

Resources