add the new row client has entered to data source on radgirdview - c#

I am new to using the telerik tools for winforms and I was wondering if anyone could help me to find the best way so that the client can add a new row on the radgrid and for it to show this change on the data source.
so far I have the radgrid set up so that the client can add a new row. I just need to bind it to the data source.
private void radGridView1_Click(object sender, EventArgs e)
{
this.radGridView1.AllowEditRow = false;
this.radGridView1.AllowAddNewRow = true;
this.radGridView1.AllowDeleteRow = false;
this.radGridView1.AddNewRowPosition = Telerik.WinControls.UI.SystemRowPosition.Top;
this.radGridView1.MasterTemplate.AddNewBoundRowBeforeEdit = true;
radGridView1.EnableAlternatingRowColor = true;
}

Have a look at the UserAddedRow event for RadGridView. This is fired after the user added a new row to the grid. You could then add the new entries to a source list or data table.
List<Foo> _lSource = new List<Foo>();
DataTable _tSource = new DataTable();
private void radGridView1_UserAddedRow(object sender, GridViewRowEventArgs e)
{
Foo foo = new Foo();
foo.Col1 = e.Row.Cells["col1"].Value.ToString();
foo.Col2 = e.Row.Cells["col2"].Value.ToString();
_lSource.Add(foo);
_tSource.Rows.Add(e.Row.Cells["col1"].Value.ToString(), e.Row.Cells["col2"].Value.ToString());
}
I added both possibilities in the same code snippet. Just choose one (list or table). I just created a random class to show you an example. You can create your own classes and name the properties as you want. Just note that the column (col1 and col2 in my example) must exist, otherwise you'll get a NullReferenceException. If you're using DataTable you have to add the columns once before adding rows.
_tSource.Columns.Add("col1");
_tSource.Columns.Add("col2");
I also recommend not to update the RadGridView properties on a click event of RadGridView. Because it is enough to set those properties once. Now you set them every time you click in your grid view. Either move them to the Load event of your form or set it directly in designer properties.
private void Form_Load(object sender, EventArgs e)
{
radGridView1.AllowEditRow = false;
radGridView1.AllowAddNewRow = true;
radGridView1.AllowDeleteRow = false;
radGridView1.AddNewRowPosition = Telerik.WinControls.UI.SystemRowPosition.Top;
radGridView1.MasterTemplate.AddNewBoundRowBeforeEdit = true;
radGridView1.EnableAlternatingRowColor = true;
}

RadGridView supports data binding to variety of data sources and CRUD operation will be handled for you automatically. Here you can find information on the supported data sources: Telerik UI for WinForms Documentation
And here is how to bind to a DataTable, where all CRUD operations work out of the box
RadGridView radGridView1 = new RadGridView();
this.Controls.Add(radGridView1);
radGridView1.Dock = DockStyle.Fill;
DataTable table = new DataTable();
table.Columns.Add("col1");
table.Columns.Add("col2");
table.Rows.Add("value1", "value1");
table.Rows.Add("value2", "value2");
radGridView1.DataSource = table;
Here is also a design time tutorial.

Related

How to bind Access database to DataTable?

I'm new to C# and Visual Studio.
I'm working on a project that is searching through a database of information.
I want to bind a database (Microsoft access file) to my datagridview
but I want it to work with my preexisting code which utilizes a datatable converted into a dataview.
My database has a lot of information and I don't want to put it in manually. I've tried binding the information directly to the datagridview (through datasource in the properties) but then searching doesn't work**. I've looked into sql but im trying to avoid learning 2 languages at the same time.
My projects basic functionality contains: 1 combobox (idCbo) containing the search query's 1 datagridview for displaying the information
this setup is for searching one column only, im going to duplicate the code for the oher columns
The name of the column in the datagridview selects the column(id) for filtering then the combo box(idCbo) searches that column for matching characters in the datagridview and comboBox list.
the combo box contains the values 1-100 for searching the column
public partial class Form1 : Form
{
DataTable dt = new DataTable();
DataView dataView;
public Form1()
{
InitializeComponent();
dt.Columns.Add("id", typeof(int));
for (int i = 0; i < 100; i++)
dt.Rows.Add(i);
dataView = new DataView(dt);
this.dataGridView1.DataSource = dataView;
}
private void idCbo_SelectedIndexChanged(object sender, EventArgs e)
{
string query = idCbo.Text;
dataView.RowFilter = $"convert(id,'System.String') LIKE '%{query}%'";
}
}
**
Binding the database to the datagridview while using this code renders column titles but not the information and the code cannot access the database, columns or the rows System.Data.EvaluateException: 'Cannot find column ...
Big thanks to Johng for assisting me with the code :)
CURRENT WORKING CODE
public Form1()
{
InitializeComponent();
}
public static BindingSource gridBindingSource;
private void idCbo_SelectedIndexChanged(object sender, EventArgs e)
{
string query = idCbo.Text;
gridBindingSource = (BindingSource)dataGridView1.DataSource;
if (gridBindingSource != null)
{
if (query == "All")
{
gridBindingSource.Filter = "";
}
else
{
gridBindingSource.Filter = "convert(id,'System.String') LIKE '%" + query + "%'";
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the '_InfoFinalv_2___CopyDataSet.Info' table. You can move, or remove it, as needed.
infoTableAdapter.Fill(this._InfoFinalv_2___CopyDataSet.Info);
idCbo.Items.Add("All");
for (int i = 1; i < 100; i++)
{
idCbo.Items.Add(i);
}
idCbo.SelectedIndex = -1;
}
private void idReset_Click(object sender, EventArgs e)
{
idCbo.SelectedIndex = -1;
}
If you have set up the grids data source in the designer “correctly” then using the DataView as you want can be simplified by using the existing BindingSource that is usually created when you set up the grid’s data source in the designer.
We can use the existing grid’s BindingSource and then use it’s Filter property as opposed to converting the BindingSource to a DataView to filter. This will allow us to set the filter in the grid WITHOUT having to “change” the grids data source.
Remove all the code you have in the form constructor obviously leaving the InitializeComponent(); and add the code below to the forms Load event. In the load event all we do is set up the combo box with the proper values. I added an “All” option to allow the user to “un-filter” the data in the grid.
private void Form1_Load(object sender, EventArgs e) {
// TODO: This line of code loads data into the 'database1DataSet.EmployeeDT' table. You can move, or remove it, as needed.
employeeDTTableAdapter.Fill(this.database1DataSet.EmployeeDT); // <- created by the designer
idCbo.Items.Add("All");
for (int i = 1; i < 100; i++) {
idCbo.Items.Add(i);
}
idCbo.SelectedIndex = 0;
}
Then in the combo boxes SelectedIndexChanged event... change the code as shown below. Cast the grids DataSource to a BindingSource and then use its Filter property.
private void idCbo_SelectedIndexChanged(object sender, EventArgs e) {
string query = idCbo.Text;
BindingSource GridBS = (BindingSource)dataGridView1.DataSource;
if (GridBS != null) {
if (query == "All") {
GridBS.Filter = "";
}
else {
GridBS.Filter = "EmpID LIKE '%" + query + "%'";
}
}
}
Here's the tip:
On the form load, make an ajax call to the database and fetch only the required data columns. Return data will be in JSON that can be used as data for DataTable.
I used it in an MVC project recently and it works fine. If you would like I can share the detailed code and logic.
Not sharing the code since I'm not sure if you are on .Net MVC.

Datatable gets reset unexpectedly

I have a datagrid which refreshes every 3 seconds and while refresh happens Datatable gets reset everytime unexpectedly. What i do is adding values from one datatable to another.
However if use 'DefaultView.ToTable' it stores the data.
Basically, I have 2 datatables dtTopSQL and dtCurTopSQL.
dtTopSQL getting new data and adding to the datatable dtCurTopSQL and I want to store the rows in dtCurTopSQL;
// Initial Load the Datatable Structure
private void Main_Load(object sender, EventArgs e)
{
dtTopSQL.Columns.Add("SQL_ID", typeof(string));
dtTopSQL.Columns.Add("Count", typeof(Int16));
dtTopSQL.Columns.Add("CurTime", typeof(DateTime));
}
// Timer start refreshing the datagrid
private void timer_TimerTopSQL(object sender, EventArgs e)
{
dtTopSQL.Clear(); // Clear before the Fill
odaTopSQL = new OracleDataAdapter(getTopSQLString, oradb);
odaTopSQL.Fill(dtTopSQL);
getTopSQL();
}
// Merging datatable starts here.
public void getTopSQL()
{
for (int i = 0; i < dtTopSQL.Rows.Count; i++)
{
bool isDupe = false;
for (j = 0; j < dtCurTopSQL.Rows.Count; j++)
{
if (dtTopSQL.Rows[i][0].ToString() == dtCurTopSQL.Rows[j][0].ToString())
{
dtCurTopSQL.Rows[j][1] = int.Parse(dtCurTopSQL.Rows[j][1].ToString()) + int.Parse(dtTopSQL.Rows[i][1].ToString());
dtCurTopSQL.Rows[j][2] = CurDate;
isDupe = true;
break;
}
}
if (!isDupe)
{
dtCurTopSQL.ImportRow(dtTopSQL.Rows[i]);
dtCurTopSQL.Rows[j][2] = CurDate;
}
}
ugTopSQL.DataSource = dtCurTopSQL; // Bind the merged Datatable.
}
Above code works if i use below before just binding data;
dtCurTopSQL = dtCurTopSQL.DefaultView.ToTable();
However i don't understand why? I want to store data without using DefaultView.ToTable
Could you explain please?
Above code works if i use below before just binding data;
dtCurTopSQL = dtCurTopSQL.DefaultView.ToTable();
However i don't understand why? I want to store data without using
DefaultView.ToTable
ugTopSQL.DataSource = dtCurTopSQL; // Bind the merged Datatable.
The first time this statement is executed, the DataSource changes from null to dtCurTopSQL. Each subsequent time it is executed, the DataSource setter checks if the new value is the same as its existing value and if so it does nothing.
By assigning a new table to dtCurTopSQL before assigning the DataSource you are assigning a new object to the DataSource, so it acts according with the new source.
A simple solution would be to add
ugTopSQL.DataSource = null;
as the first statement in the getTopSQL method.
What you are doing is absolutely unnecessary. When UltraGrid is bound to DataTable each time data table's rows change the grid will automatically show this change. So change your Load event like this:
// Initial Load the Datatable Structure
private void Main_Load(object sender, EventArgs e)
{
dtTopSQL.Columns.Add("SQL_ID", typeof(string));
dtTopSQL.Columns.Add("Count", typeof(Int16));
dtTopSQL.Columns.Add("CurTime", typeof(DateTime));
ugTopSQL.DataSource = dtCurTopSQL; // Bind the merged Datatable.
}
Then remove this row from getTopSQL method:
ugTopSQL.DataSource = dtCurTopSQL; // Bind the merged Datatable.
This way whatever changes you made to dtCurTopSQL will be automatically shown in the grid without setting the grid's DataSource, which by the way is expensive operation and should not be done each three seconds.

What is the most efficient way to sync two datagridviews?

Here is what I am trying to accomplish:
I have two datagriddviews. Datagridview1 holds a list of urls. Datagridview2 is basically a bookmarking system. A user can right-click on a url in datagridview1 and select to favorite or "bookmark" it. That row will then be copied to datagridview2. The font on the row on datagridview1 will then become bolded to show that it has been favorited and added to datagridview2. How do I keep these two synced? For example if a user removes a favorite on datagridview2 it should become unbolded on datagridview1 as it is no longer a favorite. What is an efficient way to keep these 2 synced to each other since the indexes will be different? I Had through about just iterating through each row to see if the cell content strings were equal and then unbolding it based off that but that seems very inefficient. Any suggestions on how to accomplish this?
As a good option you can have a data source like a DataTable having and string Url and a bool Favorite column.
Then, bind the first grid to the data table as its DataSource.
Bind the second grid to a DataView that you created from your data table and set Favorite=true as its Filter.
Then handle RowPrePaint event of first DataGridView and set the font to be bold if the row has favorite=true, else set the font to be regular.
For add button and remove button, set the value of favorite column on data row behind the current row and then call EndEdit on data row.
Initialization
private void Form1_Load(object sender, EventArgs e)
{
//Create DataTable
var data = new DataTable();
data.Columns.Add("Url", typeof(string));
data.Columns.Add("Favorite", typeof(bool));
//Fill Data
data.Rows.Add("http://stackoverflow.com", true);
data.Rows.Add("http://bing.com", false);
data.Rows.Add("http://google.com", false);
//Set DataBidnings to allUrlsDGV
this.allUrlsDGV.DataSource = data;
//Set DataBidnings to favoriteUrlsDGV
var favoriteData = new DataView(data);
favoriteData.RowFilter = "Favorite=true";
this.favoriteUrlsDGV.DataSource = favoriteData;
}
RowPrePaint
private void allUrlsDGV_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
var f = e.InheritedRowStyle.Font;
var drv = (DataRowView)allUrlsDGV.Rows[e.RowIndex].DataBoundItem;
if (drv.Row.Field<bool>("Favorite") == true)
allUrlsDGV.Rows[e.RowIndex].DefaultCellStyle.Font = new Font(f, FontStyle.Bold);
else
allUrlsDGV.Rows[e.RowIndex].DefaultCellStyle.Font = new Font(f, FontStyle.Regular);
}
Add and Remove Buttons
private void AddButton_Click(object sender, EventArgs e)
{
if (allUrlsDGV.CurrentRow == null)
return;
var drv = (DataRowView)allUrlsDGV.CurrentRow.DataBoundItem;
drv.Row["Favorite"] = true;
drv.Row.EndEdit();
}
private void RemoveButton_Click(object sender, EventArgs e)
{
if (favoriteUrlsDGV.CurrentRow == null)
return;
var drv = (DataRowView)favoriteUrlsDGV.CurrentRow.DataBoundItem;
drv.Row["Favorite"] = false;
drv.Row.EndEdit();
}

c# clear datagridview bound to datasource

I have a datagridview bound to a datasource, all headers are added to the columns collection with a datapropertyname set.
When I clear the datagridview using
DataGridView1.DataSource = null;
The headers disappear also and when I fill the datagridview again the header texts are the database column names. How do I clear a bound datagridview without removing the headers?
You can use this
if (dataGridViewNotas.DataSource != null)
((DataTable) dataGridViewNotas.DataSource).Rows.Clear();
One of the approach to handle this issue is to use collection as data source,
Create a class with properties representing the data source (Each property would represent a column in the database)
public class Student
{
public string Name { get; set; }
public string Address { get; set; }
}
You need Add column to datagridview manually and set relevant DataPropertyName for each column and set the HeaderText. When you load the data from database first fill this data into a List. So you will have a List<Student>.
List<Student> studentDetail = new List<Student>();
Set this as the data source of the datagridview.
dataGridView1.DataSource = studentDetail;
Clearing Data source
To clear the data source of the Grid just create a empty Student list and set it as data source again. When you set like this header of each column will be retained.
List<Student> emptyStudentDetail = new List<Student>();
dataGridView1.DataSource = emptyStudentDetail;
If you do not want to use collection of objects and still refresh your data source using this.dataGridView1.DataSource = null; then try this approach:
Assuming you are using for data source either DataSet or local database. Each time before you bind the dataGridView with new data source, create unbound columns with the same names as the names of your data source. Once they are created, you should hide them, because they will be needed when you refresh dataGridView's data source. The following sample code is aware of the data source columns names and therefore they are hard coded, but you can loop each time through the data source and create new collection of unbound columns, if it is necessary.
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.DataSource = GetDataSet();
this.dataGridView1.DataMember = "Students";
this.dataGridView1.Columns.Add("unboundColumn1", "ID");
this.dataGridView1.Columns.Add("unboundColumn2", "Name");
this.dataGridView1.Columns["unboundColumn1"].Visible = false;
this.dataGridView1.Columns["unboundColumn2"].Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.Columns["unboundColumn1"].Visible = true;
this.dataGridView1.Columns["unboundColumn2"].Visible = true;
this.dataGridView1.DataSource = null;
}
private DataSet GetDataSet()
{
DataSet dataSet = new DataSet();
dataSet.Tables.Add("Students");
dataSet.Tables["Students"].Columns.Add("ID", typeof(int));
dataSet.Tables["Students"].Columns.Add("Name", typeof(string));
dataSet.Tables["Students"].Rows.Add(1, "John Joy");
dataSet.Tables["Students"].Rows.Add(2, "Ivan Nova");
dataSet.Tables["Students"].Rows.Add(3, "Michael German");
return dataSet;
}
Struggled with this for 24 hrs. Not sure why it works, but the solution that did not produce a runtime error for me is to dispose of the table adapter associated with the datagridview:
if (this.dataTableTableAdapter != null)
{
this.dataTableTableAdapter.Dispose();
}

Infragistics Ultragrid - Combobox as column

I have a problem with the UltraGrid control from Infragistics. I have created a ultracombobox with a few values in it:
UltraCombo ultraComboPaneel = new UltraCombo();
ultraComboPaneel.DataSource = articleList;
ultraComboPaneel.ValueMember = "ArticleID";
ultraComboPaneel.DisplayMember = "Name";
Now I have an UltraGrid, and I want to put the ultraCombo in a cell so I can choose one of the items of the ultracombo as a cell value. I tried it both in code and in the ultragrid designer but i can't seem to find a way to do it.
Any of you got an idea? More information can be provided if needed
Edit:
I found something like
UltraGridColumn ugc = ultraGridTypePaneel.DisplayLayout.Bands[0].Columns.Add("combo");
ultraGridTypePaneel.DisplayLayout.Bands[0].Columns["combo"].EditorControl = ultraComboPaneel;
I feel I'm on the right way but it is still not showing on the screen...
The UltraCombo provides a great deal of functionality. If all you need is the ability to choose an item from a list, you might find the grid's ValueLists provide a better solution.
Here's some code to get you started:
private void myGrid_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
{
const string colorValueList = #"ColorValueList";
if (!e.Layout.ValueLists.Exists(colorValueList))
{
ValueList svl = e.Layout.ValueLists.Add(colorValueList);
svl.ValueListItems.Add(1, "Red");
svl.ValueListItems.Add(2, "Green");
svl.ValueListItems.Add(3, "Blue");
}
e.Layout.Bands[0].Columns["Color"].ValueList = e.Layout.ValueLists[colorValueList];
}
You could find at the link below some approaches that you could use to put a DropDown into a UltraGrid cell:
http://devcenter.infragistics.com/Support/KnowledgeBaseArticle.aspx?ArticleID=7841
Going back to your current code snippet - you are almost there:
First you should set the binding context of your UltraCombo to the BindingContext of the form the your UltraCombo will be used like:
ultraComboPaneel.BindingContext = this.BindingContext;
Please note that setting binging context should happen prior setting your control to be EditorControl. One more thing that I noticed is that the property currently is changed to EditorComponent so I believe that you are using older version of the Infragistics components. However you should still be able to use the very same approach. I have created small code snippet showing the above with code:
public partial class Form1 : Form
{
UltraCombo uc;
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
dt.Columns.Add("Int", typeof(int));
dt.Rows.Add(1);
dt.Rows.Add(1);
dt.Rows.Add(1);
DataTable dtt = new DataTable();
dtt.Columns.Add("Int", typeof(int));
dtt.Rows.Add(2);
dtt.Rows.Add(2);
dtt.Rows.Add(2);
uc = new UltraCombo();
uc.BindingContext = this.BindingContext;
uc.DataSource = dtt;
ultraGrid1.DataSource = dt.DefaultView;
}
private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
{
e.Layout.Bands[0].Columns[0].EditorComponent = uc;
}
}
Hope this helps.
I use the Ultra Dropdown instead.
dim udd As UltraDropDown
udd = New UltraDropDown
With udd
'add data binding or value list items here
End With
Me.ultragrid.DisplayLayout.Bands(0).Columns("Column Name").ValueList = udd
The key is the last line that assigns the "Value List" of the ultra grid column to the Drop down control.

Categories