This filters the datatable correctly:
DataView dv2 = dT.DefaultView;
dv2.RowFilter = "myfiled = " + "1";
dT = dv2.ToTable();
after this:
dT.DefaultView.RowFilter = string.Empty;
The datatable appears empty, I do not understand why.
You can simply use null:
dT.DefaultView.RowFilter = null;
Ok, i solving. Declare DefaultView and dispose her out of for loop and work fine.
Related
**hello
Error adding items to the Grid.
please guide me**
Model1Container Mobl = new Model1Container();
JadvalSabtenam Sabt = new JadvalSabtenam();
Sabt.name = TextBox1.Text;
Sabt.family = TextBox2.Text;
Mobl.AddToJadvalSabtenamSet(Sabt);
Mobl.SaveChanges();
GridView1.DataSource = Sabt;
GridView1.DataBind();
GridView1.DataSource = Sabt;
GridView1.DataBind();
Sabt seems to be single object. To bind DataGrid you need to have collection like List<JadvalSabtenam > or BindingList<JadvalSabtenam>
Try that:
List<JadvalSabtenam > dataSource = new List<JadvalSabtenam> {Sabt};
GridView1.DataSource = dataSource;
GridView1.DataBind();
Reason for that is: What Grid should do with single object? When it's collection each item is corresponding to one row, and each property to column which makes perfect sense.
thank you. very well.
My problem was solved with your code.
List<JadvalSabtenam> dataSource = new List<JadvalSabtenam> { Sabt };
GridView1.DataSourceID = "";
GridView1.DataBind();
I have a DataTable that I make a DataView with. In the Dataview, I filter and sort the info from the first DataTable. Then I tried to make a new DataTable out of the DataView where I sorted and filtered the content.
Everything seems to work alright but the new DataTable is null, so I lost the content from the original DataTable and the rest of the function does not work anymore. Also, I am getting an Unreachable Code warning in File B # System.Data.DataTable dt480 = dv.ToTable();
I'll keep trying to solve this but I'd like to see if I am way off, or on the right track. Thanks in advance.
I have two files where the code is stored.
File A:
static public void ElecOneLine1(System.Data.DataView dv)
{
string Path = Commands.LoadFile();
System.Data.DataTable table = Commands.ReadExcelToTable(Path);
Commands.ElecDv480V(table);
//Commands.OneLineDt(dv);
Commands.InsertElec1LineBlockscd();
Commands.DrawOneLineBuscd("OneLineBackground", 35.5478, 23.3750, 0, table.Rows[0]);
for (int i = 1; i < 8; i++)
{
Commands.DrawOneLineItem("" + table.Rows[1 + i][0], 4 + ((double)i * 3), 12.6835, 0, table.Rows[0], i + 1);
}
}
File B (called "Commands"):
public static DataView ElecDv480V(System.Data.DataTable dt)
{
System.Data.DataView dv = new DataView(dt);
dv.RowFilter = "F1 = '480V'";
dv.Sort = "F2 ASC, F3 ASC";
return dv;
System.Data.DataTable dt480 = dv.ToTable();
}
return should be the last statement.The return statement terminates execution of the method in which it appears
public static DataView ElecDv480V(System.Data.DataTable dt)
{
System.Data.DataView dv = new DataView(dt);
dv.RowFilter = "F1 = '480V'";
dv.Sort = "F2 ASC, F3 ASC";
System.Data.DataTable dt480 = dv.ToTable();
return dv;
}
Also,You are returning a dataview so you need to have a variable at receiving end also which you are missing.Beside what is the use of setting values for dt480
I have the following code which doesn't seem to work. In the Page_Load function I populate the DataSet and display the results in a grid view.
newsCommand = new SqlCommand("SQL code here", dbConnection);
newsDataSet = new DataSet();
newsDataAdapter = new SqlDataAdapter(newsCommand);
newsDataAdapter.SelectCommand = newsCommand;
newsDataAdapter.Fill(newsDataSet, "Bulletins");
if (!Page.IsPostBack)
{
GridViewMain.DataSource = newsDataSet;
GridViewMain.DataBind();
}
I have some links which call this function to filter the data (yearID is passed as a parameter):
DataTable newsTable = new DataTable();
newsTable = newsDataSet.Tables[0];
DataView dvData = new DataView(newsTable);
dvData.RowFilter = "Year > '" + yearID + "'";
GridViewMain.DataSource = dvData;
GridViewMain.DataBind();
Yet the gridview shows the data it orignally loaded, and not the filtered data. The only thing I can think of is that I'm not using the DataTable in the Page_Load function. What else am I missing?
Thanks,
Adrian
Changed the code in the function to:
DataView dataView = newsDataSet.Tables[0].DefaultView;
dataView.RowFilter = "NewsDate2 Like '%" + yearID + "'";
GridViewMain.DataSource = dataView;
GridViewMain.DataBind();
It must have been something in the RowFilter statement.
Filter data from DataTable and display it in Gridview.
string category = ddlcat.SelectedItem.Value; // this can be any input by user
DataTable dt = filter_dt; //filter_dt is DataTable object, contains actual data, from there we will filter
DataView dataView = dt.DefaultView;
if (!string.IsNullOrEmpty(category))
{
dataView.RowFilter = "Category = '" + category + "'";
}
Gridview1.DataSource = dataView;
Gridview1.DataBind();
If any doubt,feel free to ask.
Thank you
i have a DataTable that has a column ("Profit"). What i want is to get the Sum of all the values in this table. I tried to do this in the following manner...
DataTable dsTemp = new DataTable();
dsTemp.Columns.Add("Profit");
DataRow dr = null;
dr = dsTemp.NewRow();
dr["Profit"] = 100;
dsTemp.Rows.Add(dr);
dr = dsTemp.NewRow();
dr["Profit"] = 200;
dsTemp.Rows.Add(dr);
DataView dvTotal = dsTemp.DefaultView;
dvTotal.RowFilter = " SUM ( Profit ) ";
DataTable dt = dvTotal.ToTable();
But i get an error while applying the filter...
how can i get the Sum of the Profit column in a variable
thank you...
Set the datacolumn to a numeric type (int, decimal, whatever):
DataColumn col = new DataColumn("Profit", typeof(int));
dsTemp.Columns.Add(col);
Use Compute:
int total = dsTemp.Compute("Sum(Profit)", "");
Note that aggregation is not a type of filter, which is the main problem with the approach you are tyring.
I used the DataTable's Compute method as suggested, and it works fine. I apply the same filtering as I use for the DataView (which is used to display the sorted and filtered data in the data grid) together with an aggregate function.
string SingleVocheridSale ="1";
DataView view = new DataView(dt);
view.RowFilter = string.Format("VOCHID='" + SingleVocheridSale + "'");
dataGridview1.DataSource = view;
object sumObject;
sumObject = dt.Compute("Sum(NETAMT)", view.RowFilter);
lable1.Text = sumObject.ToString();
I have a DataTable. I'd like to sort its default view by a column name 'city'. I'd like the sort to be case-insensitive.
Here is the code I have:
DataTable dt = GetDataFromSource();
dt.DefaultView.Sort = "city asc";
MyReport.DataSource = dt.DefaultView;
Thanks.
Never mind. It's in the documentation.
DataTable dt = GetDataFromSource();
dt.CaseSensitive = false;
dt.DefaultView.Sort = "city asc";