How to properly fetch datagridview - c#

In my WinForms app, i've three tabs and in each tab a gatagridview.
If i click a btn to display records in tab1 datagridview, it populates BUT if i go to tab2 and hit a button to display a different record, it first displays me the right records and then dispalys me the same records in tab1 or tab3 if a different button is clicked.
It then populate me the same record from tab1,tab2 and tab3.
How can i solve this problem OR is it that i declared a globale Datatable dt variable?

If you want the 3 DGVs to have each his own record pointer you must not use the DataTable as their DataSource directly. Instead use an intermediate BindingSource for each of them:
// assume a few DataGridViews..:
DataGridView DGV1 = new DataGridView();
DataGridView DGV2 = new DataGridView();
DataGridView DGV3 = new DataGridView();
// and a common DataTable:
DataTable DT = new DataTable();
//..
// we need a separate BindingSource for each DGV:
BindingSource BS1 = new BindingSource();
BindingSource BS2 = new BindingSource();
BindingSource BS3 = new BindingSource();
// each is bound to the DataTable
BS1.DataSource = DT;
BS2.DataSource = DT;
BS3.DataSource = DT;
// now we set them to be the DatSource of the DGVs:
DGV1.DataSource = BS1;
DGV2.DataSource = BS2;
DGV3.DataSource = BS3;
// now we can set the record pointers separately:
BS1.Position = 3;
BS2.Position = 0;
BS3.Position = BS3.Count - 1;
// or set filters:
BS2.Filter = "someCondition";
// or set sorts:
BS3.Filter = "someSort";

Related

Modifying a column in DGV to be a Combobox Bound to DataBase

I created a view named "FamilyView" to join 2 tables(Families and SStatus) and replace an id in the first table(Families) by its name existing in the second table(SStatus) (the column is now called "Status")
This view is now displayed in a datagridview that the user can modify
Sda = new SqlDataAdapter("Select * from FamilyView",con);
Sda.Fill(ds);
dg.DataSource = ds.Tables[0];
dg.Refresh();
What i want is to transform the column cells "Status" to comboboxes containing all Names from SStatus
SqlDataAdapter sadapter = new SqlDataAdapter("Select * From SStatus", con);
DataSet ds1 = new DataSet();
sadapter.Fill(ds1);
i found this code:
DataGridViewComboBoxCell ComboColumn = (DataGridViewComboBoxCell)(dg.Rows[i].Cells[0]);
ComboColumn.DataSource = ds1.Tables[0];
ComboColumn.DisplayMember = "Name";
but i can't replace the cell
and this code but I'm not sure how to use it:
Adding bound combobox to datagridview
BindingSource StatusBD = new BindingSource();
StatusBD.DataSource = ds1;
DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn();
colType.HeaderText = "Status";
colType.DropDownWidth = 90;
colType.Width = 90;
//colType.DataPropertyName = "Name";
colType.DataSource = ds;
colType.DisplayMember = "Name";
colType.ValueMember = "IdStatus";
dg.Columns.Insert(dg.Columns.GetColumnCount(DataGridViewElementStates.None) - 1, colType);
Your data source is empty: it should contain 2 tables: one with the SStatus and one with Families.
and then follow my as in your post:
DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn();
BindingSource wizardBindingSource = new BindingSource();
wizardBindingSource.DataSource = dataSet;
wizardBindingSource.DataMember = "protocol"; // This is the table in the set
colType.HeaderText = "Type";
colType.DropDownWidth = 90;
colType.Width = 90;
colType.DataPropertyName = "wizardProtocol";
colType.DataSource = wizardBindingSource;
colType.DisplayMember = "protocolName";
colType.ValueMember = "idprotocols"
Here idProtocol (which is referenced as protocol in the "pcap" table) is mapped to protocolName.

Is It Possible to Add Multiple Repository Items in a Gridview Cells using DEVEXPRESS?

I have Datatable and I have to add Two repository items to same cell
DataTable dtbl = null;
public Form1()
{
InitializeComponent();
dtbl = new DataTable();
dtbl.Columns.Add("T1");
dtbl.Columns.Add("T2");
dtbl.Columns.Add("T3");
dtbl.Rows.Add("Test1", "Test2", "Test3");
dtbl.Rows.Add("Test4", "Test5", "Test6");
dtbl.Rows.Add("Test7", "Test8", "Test9");
gridControl1.DataSource = dtbl.DefaultView;
gridView1.CustomRowCellEdit += new DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventHandler(gridView1_CustomRowCellEdit);
}
void gridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
{
RepositoryItemButtonEdit rebBtn = new RepositoryItemButtonEdit();
RepositoryItemComboBox rebcmb= new RepositoryItemComboBox();
rebBtn.AutoHeight = false;
rebcmb.AutoHeight = false;
e.RepositoryItem = rebBtn;
e.RepositoryItem = rebcmb;
}
I have to add Button and Combobox is it Possible .
The image i uploaded is a WPF gridColumn , is this possible in DEVEXPRESS GridControl
This is Devexpress Gridcontrol .. here i created Two sepetate columns..
You can't have two RepositoryItems active at the same time. You could use a ComboBox RepositoryItem and add an additional button to the Buttons property though. You could then react to the ButtonClick or ButtonPressed events.

Value from DataTable into gridview as combobox

I am trying to get one column from my datatable to be displayed as a combo box in my grid view. Also need to have the ability to fill the combo with a small collection to chose from. It will not show the values when i bind it from the Gui so I am trying it programmatic but cant seem to find the right code.
connection2 = new MySqlConnection(ConnectionString2);
try
{
string proid = txtbxProjId.Text;
//prepare query to get all records from items table
string query2 = "select sl.sample_id As sample_id, sl.sample_number As Sample_Number, sl.sample_date As Sample_Date, sl.sample_time As Sample_Time, sl.sample_comments As Sample_Comments FROM spt_sample_login sl Where project_id = '"+proid+"'";
//prepare adapter to run query
adapter2 = new MySqlDataAdapter(query2, connection2);
adapter3 = new MySqlDataAdapter(query2, connection2);
//create a DataTable to hold the query results
DataTable dTable2 = new DataTable();
DataSet DS2 = new DataSet();
//fill the DataTable
//get query results in dataset
adapter2.Fill(dTable2);
adapter3.Fill(DS2);
//return datatable with all records
//BindingSource to sync DataTable and DataGridView
//set the BindingSource DataSource
GridView1.DataSource = dTable2;
this.GridView1.Columns["sample_id"].Visible = false;
this.GridView1.Columns["Sample_Type"].DisplayIndex = 4;
this.GridView1.Columns["Sample_Type"].Visible = true;
//set the DataGridView DataSource
}
catch (MySqlException ex)
{
}
}
This works but shows the Sample_Type as a text box where i want it to be a combo with F,P Q,B as options.
Thank you
Brent
You have to put your ComboBox in a itemTemplate, and you have to fill it during rowDataBound event.
Quick exemple from my current project:
DropDownList ddlRole = (DropDownList)e.Row.FindControl("ddlRole");
ddlRole.DataSource = GetTruckersRoles();
string[] rolesList = Roles.GetRolesForUser((string)gvTruckers.DataKeys[e.Row.RowIndex][0]);
if (rolesList.Length > 0)
{
//If user is not in any roles, dont' do this
ddlRole.SelectedValue = rolesList[0];
}
ddlRole.DataBind();
Just adapt the code to your situation

Duplicate column in DataTable when adding row

I create a DataTable and bind it to a DataGrid. My DataSource consist of one Table (FooTable) which consist of one column (FooName).
The following codes runs fine - except that each time I add a new row, there is a duplicate Column fill in with the same data, which I don't know how to get rid of. See below image and code. I have only one FooName column and a duplicate column comes out.
/* Create a DataGrid dg1 */
DataGrid dg1 = new DataGrid();
DataGridTextColumn col = new DataGridTextColumn();
col = new DataGridTextColumn();
colA.Binding = new Binding("FooName");
colA.Header = "FooName";
dg1.Columns.Add(colA);
dataGrid1.Children.Add(dg1);
/* Create a DataTable and bind it to DataGrid */
SqlCeDataAdapter da = new SqlCeDataAdapter();
string sqlStr = #"SELECT * FROM FooTable";
da.SelectCommand = new SqlCeCommand(sqlStr, conn);
da.Fill(ds, "FooTable");
dt = ds.Tables["FooTable"];
DataRow newRow = dt.NewRow();
newRow["FooName"] = "Donkey";
dt.Rows.Add(newRow);
dg1.ItemsSource = ds.Tables[0].DefaultView;
try setting AutoGenerateColumns to false
dg1.AutoGenerateColumns = false
Try
dg1.AutoGenerateColumns = false;
Should do the work for you. For now the datagrid automatically generates the columns AND adds the one you asked

Adding a comboBox to a gridview in WinForms

I am trying to create a gridview with a string column, a checkbox column, and a dropdownlist/combobox column. The first two are finished (all code behind), just need help with the last one.
DataTable dt = new DataTable("tblAir");
dt.Columns.Add("Flight Details", typeof(string));
dt.Columns.Add("Prefered Seating", typeof(bool));
//doesn't work
dt.Columns.Add("Add Remark", typeof(ComboBox));
The data for the combobox is being supplied on load as we cannot work with a database.
Peter Bromberg has a detailed article on creating a Winforms gridview with comboboxes:
http://www.eggheadcafe.com/articles/20060202.asp
DataAccessLayer dal = new DataAccessLayer();
DataTable movies = dal.GetMovies();
gvMovies.DataSource = movies;
gvMovies.AllowUserToAddRows = false;
gvMovies.AllowUserToDeleteRows = false;
//Create the new combobox column and set it's DataSource to a DataTable
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.DataSource = dal.GetMovieTypes(); ;
col.ValueMember = "MovieTypeID";
col.DisplayMember = "MovieType";
col.DataPropertyName = "MovieTypeID";
//Add your new combobox column to the gridview
gvMovies.Columns.Add(col);

Categories