I want to hide a data row without deleting it in c# or vb.net. Please help me with this.
I want to hide a data row without deleting it in c# or vb.net.
ResultSelection.Checked = False ' Checkbox
Dim Dt As DataTable
Dim dr as datarow
Private SBind As BindingSource = New BindingSource()
With Dt
.Columns.Add(" Name", GetType(String))
.Columns.Add("Age" , GetType(integer))
.Columns.Add("Marks" , GetType(integer))
dr(0) = ( "Mark",19,99)
dr(1) = ( "Rahul",20,35)
dr(2) = ( "Steve",19,50)
SBind.DataSource = Dt
DatargridView.DataSource = SBind
if ResultSelection.Checked = False then
"entire row dr(2) should not be visible
else
"entire data table should be visible along with dr(2)
end if
You can't hide rows in a DataTable but you can in a DataView. Every DataTable has a DataView associated with it by default, in its DefaultView property. When you bind a DataTable, it is actually the DefaultView that the data comes from. As such, you can set the RowFilter of that DataView in order to hide rows that don't match certain criteria. For instance, if you had a Sex column that contained "Male" or "Female", you could hide all the "Male" rows like this:
myDataTable.DefaultView.RowFilter = "Sex = 'Female'"
Any controls bound to that DataTable would not see the "Male" rows and you could loop through the DataView code to see the filtered data too.
If you are binding, I would recommend using a BindingSource in between, in which case you set its Filter property in the same way.
Related
I faced to issue of displaying datarows from DataTable from DataSet. I used a temporary table. I found rows that I need by id and copy it to temporary table and then diplay in DataGridView. Is there any way to find and display rows that I need in DataGridView directly from DataTable from DataSet without using any temporary DataTable's? I've tried to do this by hiding and unhiding rows in DataGridView, but sadly it didn't worked out
private DataTable tblFiltered =
ds.Tables("GRAPHICS").AsEnumerable().Where(row =>
row.Field<int>("GRAPHIC_ID") == graphId && row.Field<int>
("GRAPHIC_ID")).CopyToDataTable;
Assuming that DataSet 'ds' containing Table 'GRAPHICS',We can use DataView to Display filtered records in DataGridView. Ex.
int graphId = 2; //Assuming
DataView dv = ds.Tables["GRAPHICS"].AsDataView();
dv.RowFilter = "GRAPHIC_ID = " + graphId; //RowFilter :: Expression used to filter which rows are viewed !!
dataGridView.DataSource = dv.ToTable();
You can use filter
(DatagridView.DataSource as DataTable).DefaultView.RowFilter = string.Format("GRAPHIC_ID= '{0}'", graphId);
I'm beginner in .Net ,so maybe my question will seem naive to some of you.
I have DataGridView table in WinForm project:
It contain three columns(image,combobox and textBox columns).
Any idea how to create and attach rows to this table?
Thank you in advance!
You create a data source, then bind the data source to the grid's DataSource property. You then add a record to your data source.
// create data source
BindingList<Shape> dataSource = new BindingList<Shape>();
// add record to data source
dataSource.Add(new Shape("Some Contour", "Circle", "Some Name"));
// bind data source
yourDataGridView.DataSource = typeof(BindingList<Shape>);
yourDataGridView.DataSource = dataSource;
Set the DataPropertyName of each column to matches the names of the fields in your Shape class.
DataGridViewTextBoxColumn colName = new DataGridViewTextBoxColumn();
colName.DataPropertyName = "Name";
yourDataGridView.Columns.Add(colName );
However, I recommend you use Virtual Mode instead to keep your data separate and decoupled.
If you wish to accept inputs from user, you have to create a form on this page using which the user can provide inputs. Take those values and add them to a DataTable. Following is a sample snippet showing it:
DataTable dt = new DataTable();
dt.Columns.Add("Contour",typeof(string)); //I am assuming that you will store path
//of image in the DataTable
dt.Columns.Add("Shape",typeof(string));
dt.Columns.Add("Name",typeof(string));
Keep adding new rows to the DataTable as you receive inputs from the user:
DataRow row = dt.NewRow();
row["Contour"] = txtContourPath.Text;
row["Shape"] = ddlShape.SelectedValue;
row["Name"] = txtName.Text;
dt.Rows.Add(row);
Assign above DataTable to DataSource property of the GridView.
dgv.DataSource = dt;
You can use method:
dataGridView1.Rows.Insert(...)
dataGridView1.Rows.Add(...)
Jay's answer : use dataGridView1.DataSource = dataSource;
Hope I can help you.
Where I can get column names if i flled gridview by DataSource dynamically?
OracleDataAdapter adapter = new OracleDataAdapter();
DataTable tableD = new DataTable();
tableD.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.SelectCommand = oracleCom;
adapter.Fill(tableD);
tableResults.DataSource = tableD.AsDataView();
tableResults.DataBind();
Where tableResults is GridView.
This code doesn't work:
updatingAtributes += tableResults.Columns[i].HeaderText;
Columns property of GridView will be set if you configure the columns on GridView. If you are relying on default behavior of GridView to render columns by setting AutoGenerateColumns property to true, then the Columns collection is not set with any value. In such case, you will have to use the object that you used as DataSource of the GridView (which is the DataTable tableD in your case) to get the column name.
I have a DataGridView and I am populating it through a DataTable. On initializing, I add DataColumns to the DataTable and then set the DataSource of the DataGridView to the DataTable. Here is the code:
DataTable mTable = new DataTable("Test");
DataColumn col = new DataColumn;
col.DataType = System.Type.GetType("System.Boolean");
col.ColumnName = "First";
col.ReadOnly = false;
mTable.Columns.Add(col);
col = new DataColumn;
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "Second";
col.ReadOnly = false;
mTable.Columns.Add(col);
this.myGridView.DataSource = mTable;
This works fine however, I want to make one of the columns showup as a combobox. I think I have to do something with the Column's datatype but I am not sure how to do this. Can anyone give me any pointer towards this?
This is old, but I thought I'd try to answer it anyways since I had the same question myself a while ago.
First of all, you cannot make a DataTable column of type DataGridViewComboBoxColumn. DataColumn inherits from System.Data, while DataGridView*Columns inherit from System.Windows.Forms.
What you can try is this: Once your DataTable is bound to your DataGridView, the columns from the DataTable should show up on the DataGridView in the designer. If you then expand the Columns property of your DataGridView, you should see your columns from your DataTable have been added to the list. Select the column that you want, and in the right-hand pane there will be a property called 'ColumnType'. The default type is DataGridViewTextBoxColumn, but you can change it to DataGridViewComboBoxColumn.
change the declaration to this:
DataGridViewComboBoxColumn column1 = new DataGridViewComboBoxColumn()
I am using C# and .NET 3.5 and have a GridView that I am setting the dataSource programatically in the code-behind page. I have data in a DataTable and then depending on a column value (isValid boolean) of each Row, I create a new row using DataRowView.AddNew() method into 1 of 2 DataViews - dvValid or dvInvalid. I am NOT creating a new DataTable.NewRow to add to the DataView Table. Then I bind the GridView to the appropriate dataView.
There is a problem when I am sorting the GridView. I am having a problem with 1 row not being sorted correctly, all other rows are sorted fine. I debugged my code and found that the DataView.Count is 1 more than the DataView.Table.Rows.Count even though I am calling DataView.Table.AcceptChanges() method. This is strange since the dataTable should have all committed rows and therefore the counts should be the same.
So why are the 2 counts different? A DataView is a subset of the DataTable so should it not have equal or less rows than the DataTable.
When I populate the DataView, should I first create the DataTables rather than creating the DataView directly? Right now, I am directly creating a DataRowView without a dDataTableRow, is this the correct approach?
Thanks for your help.
Code snippet : C#
...
//get the data as DataTable
members = GetMemberDataTable ();
//create views from a new DataTable with no rows
dvValidMembers = new DataView (CreateMembersDT("ValidMembers"));
dvInValidMembers = new DataView (CreateMembersDT("InvalidMembers"));
//iterate thru each row and put into appropriate DataView
foreach (DataRow memberRow in members.Rows)
{
if ((bool)memberRow["isValid"])
//Add to valid members Dview
member = dvValidMembers.AddNew();
else
//add to InValid members Dview
member = dvInvalidMembers.AddNew();
member["memberID"] = memberRow["memID"];
} //foreach
dvInvalidMembers.Table.AcceptChanges();
dvValidMembers.Table.AcceptChanges();
}
private System.Data.DataTable CreateMembersDT ( string tableName)
{
System.Data.DataTable dtMembers = new System.Data.DataTable(tableName);
dtMembers.Columns.Add(new DataColumn("memID", typeof(int)));
return dtMembers;
}
That 1 row that isn't sorting right, could that be the last row?
I think you are missing a DataView.EndEdit():
foreach (DataRow memberRow in members.Rows)
{
DataView dv;
if (...)
//Add to valid members Dview
dv = dvValidMembers;
else
dv = dvInvalidMembers;
member = dv.Addnew();
member["memberID"] = memberRow["memID"];
dv.EndEdit();
}
But I would also like to note that you could probably use 2 Views with a Filter on isValid and then you would only need to point them to the original members table.