How to insert multiple dropdownlistbox in gridview? - c#

GridView displays data from different tables so i can't do like there enter link description here or there enter link description here
How it do dynamically?
There my code where I bind GridView:
OracleCommand oracleCom = new OracleCommand();
oracleCom.Connection = oraConnect;
oracleCom.CommandText = "Select * From " + Session["tableNameIns"];
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();
Examples:
1. Table Objects: dropdownlist for column object_type.
2. Table Details: dropdownlist for columns point, object, patch.
3. ...

check here Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List

For drop down list in grid view for editing mode you should set tag EditItemTemplete and binding this grid view from database you should handle RowDataBound event, from this event you can bind drop down list from different tables. And this all are described in the article already.
Just one suggestion that download the source code from reference link try to debug by applying break points to understand briefly...

Related

How do i bind a bindingsource created from stored procedure to a gridview

Firstly, I have 3 tables in my database:
Student
StudentID
Name
Adress
OtherInfo
TuitionFee
StudentID
Years
Semester
FeeAmount
Detailed_TuitionFee
StudentID
Years
Semester
PaymentDate
PaymentAmount
Then I created a stored procedure to select some column on [TuitionFee ] and [Detailed_TuitionFee ] with StudentID as parameter.
create procedure
#StudentID
as
begin
select
from [TuitionFee ]
left outer join [Detailed_TuitionFee ] on TuitionFee.StudentID = Detailed_TuitionFee.StudentID
and TuitionFee.Semester = Detailed_TuitionFee.Semester
where TuitionFee.StudentID = #StudentID
and Detailed_TuitionFee.StudentID = #StudentID
In C#, the code I wrote is
BindingSource bdsTuitionInfo = new BindingSource();
String studentID;
GridControl gCtrlTuition;
GridView gViewTuition;
String sqlCmd = "exec Stored procedure" + studentID;
DataTable dt = Program.ExecSqlDataTable(sqlCmd); // create a Data table from SP
bdsTuitionInfo.DataSource = dt; // import data table into Binding source
gCtrlTuition.DataSource = bdsTuitionInfo; // import Binding source into Grid Control
gViewTuition.DataSource = bdsTuitionInfo; // The code shows ERROR: "Property or indexer 'property' cannot be assigned to -- it is read only" here
How do I show the data from this BindingSource in the GridView?
Is there anything to adjust or configure in my GridView?
I've never used DevExpress' grid but their official example appears to indicate that a GridView is not something you need to assign a DataSource on; you assign the source to the GridControl and then maybe access the GridView via the GridControl to set other properties..
gridControl1.DataSource = DataHelper.GetData(30);
// The grid automatically creates columns for the public fields found in the data source.
// Calling the gridView1.PopulateColumns method is not required unless the gridView1.OptionsBehavior.AutoPopulateColumns is disabled
// The grid automatically creates a GridView that presents the underlying data as a two-dimensional table.
GridView gridView1 = gridControl1.MainView as GridView;
GridView;
// Obtain created columns.
GridColumn colCompany = gridView1.Columns["CompanyName"];
...
at first fill your data into datatable dt. then just add GridView control on your page. then use (if you use ASP.NET Web Form):
GridView.DataSource = dt;
//GridView.Databind(); //just in asp.net webform
There is a Grid View control in ASP.NET Web Form and I think you use it.

c# datagridview fails when changing source of combobox

I've created a simple winform with one databound datagridview. The user should be able to change values in the grid. One column of this grid is a combobox called comb1. The source of comb1 is a select to database. Everything works very well.
But.....
After deleting one entry in the source of the combobox comb1 whitch is already used in the datagridview, the datagridview fails when opening. The combobox tries to validate the value in the datagridviewcolumn comb1. But this is not possible, because one entry of the combobox-source is deleted.
Error: The datagridviewcomboboxcell-value is not valid.
Hope, you can understand....
How can I fix this?
Thanks.
SqlDataAdapter sda = new SqlDataAdapter("SELECT T_Artikel.ArtikelBezeichnung, T_Artikel.IDArtikel AS Expr1 FROM T_Benutzer_Artikel INNER JOIN T_Artikel ON T_Benutzer_Artikel.IDArtikel = T_Artikel.IDArtikel WHERE(T_Benutzer_Artikel.IDBenutzer =" + lokIDBenutzer + ")", con);
DataTable dt = new DataTable();
sda.Fill(dt);
iDArtikelDataGridViewComboBoxColumn.DataSource = dt;
iDArtikelDataGridViewComboBoxColumn.ValueMember = "Expr1";
iDArtikelDataGridViewComboBoxColumn.DisplayMember = "ArtikelBezeichnung";
This piece of code supplies the source of the combobox when opening the form. After deleting one entry in table T_Artikel the combobox-source is missing this entry und fails when opening the DGV.
Hope, this helps.

how to remove (not hiding) columns from a DevExpress gridView?

I load my data into a GridView using DataSet. At first databinding, it works well. But when I run query that return different columns, the GridView show the new column plus the old one. I've tried the gridView1.Columns.Clear() method, but it doesn't solve the problem.
Here are some of the code:
// loading data into dataset
dataSet dsGrid = new dataSet();
string dtMember = "kpi";
// note: "thequery" is query generated based on user's selection
using (MySqlDataAdapter da = new MySqlDataAdapter(thequery, myCn))
{
da.Fill(dsGrid, dtMember);
}
// set the gridControl's datasource
gc_report.DataSource = null;
// clear the columns
gridView1.Columns.Clear();
// bind the data
gc_report.DataSource = dsGrid;
gc_report.DataMember = dtMember;
When I run my application, it runs well, but if the query return different column, the old column still appear although I've put the Clear() method of the gridView1.Columns collection.
How to set the gridView1 , so the grid always shows the current query?

BindingSource with DataGridView Combo Box

I know you can use the BindingSource object with a DataGridView.
Is it possible to have a combo box in one of the columns and still take advantage of the BindingSource?
Yes, it is - take a look at ComboBox with DataGridView in C#:
Using ComboBox with DataGridView is not that complex anymore but it’s almost mandatory while doing some data driven software development.
I have created a DataGridView like this one. Now, I want to show “Month” and “Item” instead of “MonthID” and “ItemID” in DataGridView.
Essentially what the article describes is binding the comboboxes with a separate binding source - in this case, a validation table, where MonthID and MonthName are stored, and the month name is displayed based on the id from the original data.
Here he sets up the Month data source, selecting from a month table, and then creates a BindingSource from the returned data.
//Month Data Source
string selectQueryStringMonth = "SELECT MonthID,MonthText FROM Table_Month";
SqlDataAdapter sqlDataAdapterMonth = new SqlDataAdapter(selectQueryStringMonth, sqlConnection);
SqlCommandBuilder sqlCommandBuilderMonth = new SqlCommandBuilder(sqlDataAdapterMonth);
DataTable dataTableMonth= new DataTable();
sqlDataAdapterMonth.Fill(dataTableMonth);
BindingSource bindingSourceMonth = new BindingSource();
bindingSourceMonth.DataSource = dataTableMonth;
Then he adds the month ComboBoxColumn to the DataGridView, using the DataSource as the BindingSource created above:
//Adding Month Combo
DataGridViewComboBoxColumn ColumnMonth = new DataGridViewComboBoxColumn();
ColumnMonth.DataPropertyName = "MonthID";
ColumnMonth.HeaderText = "Month";
ColumnMonth.Width = 120;
ColumnMonth.DataSource = bindingSourceMonth;
ColumnMonth.ValueMember = "MonthID";
ColumnMonth.DisplayMember = "MonthText";
dataGridViewComboTrial.Columns.Add(ColumnMonth);
And then finally, he binds the DataGridView to the original data.

Single datagridview row updating

Could you help with such problem:
I have two forms, datagridview and SQL database.
On Load event of my first form I'm select some data from my SQL database by using stored procedure(select query).
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("PROC001", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
datagridview.DataSource = dt;
After that, I can filter or sort my datagridview by using dt.DefaultView.Filter = .... and display in my grid only filtered rows.
On CellMouseDoubleClick event my second Form2 appearing. In this form I update some value in database by clicking on Button1 and also after that I want to update my selected datagridview row. My quetions are:
1) How can I update only selected row in datagridview and do not execute stored procedure for all datagridview filling again.
2) My datagridview is already filtered, so if I execute procedure again, my filter has been disapeared. How can I avoid of this?
3) On Form2 I'm updating some database value, that is not included in my "select query" as selected field, but this value is affected on this query. Example:
SELECT Name, SecondName FROM tUsers
WHERE id = (SELECT DISTINCT id FROM tProcedures WHERE Code = 'First')
In my datagridview I can see Name and SecondName, but in Form2 I'm updating Code in tProcedures database table. So after updating I want to see my new data in datagridview, only in selected row and with current filter. I don't want to SELECT all data again to datagridview and broke my filter.
Is it possible to update single row? Could you provide some examples?
Because the DataGridView is using DataBinding, you have to update the underlying data source, in this case the DataTable. See How to: Edit Rows in a DataTable for how to do that.
For the filter issue, you would want to save and restore the filter:
var filter = dt.DefaultView.RowFilter;
UpdateData();
dt.DefaultView.RowFilter = filter;

Categories