add text field inside datagrid - c#

i want add text field to datagrid but doesnot work how can i make it work it view text field in datagrid
public void Textboxfielld()
{
DataGridViewTextBoxColumn textBoxColumn = new DataGridViewTextBoxColumn();
textBoxColumn.HeaderText = "Quantity";
textBoxColumn.Width = 50;
textBoxColumn.Visible = true;
salesStockBunifuDataGridView.Columns.Add(textBoxColumn);
}
i call it it show only headertext
i want add textfield inside datagrid

Related

Checkbox column not getting checked in datagridview c#

I have added checkbox column in datagridview and i want that checkboxes checked if one of my column value == 1 otherwise the checkbox should be unchecked
i have written following code where content loads in datagridview and that method is called at form load event
but checkboxes not showing checked when first time that method is calling when method gets called second time that is working correct
DataGridViewCheckBoxColumn chkboxcolumn = new DataGridViewCheckBoxColumn();
chkboxcolumn.HeaderText = "";
chkboxcolumn.Width = 30;
chkboxcolumn.Name = "checkBoxColumn";
if (!dgvCompany.Columns.Contains(chkboxcolumn.Name))
{
dgvCompany.Columns.Insert(0, chkboxcolumn);
}
for (int i = 0; i < dgvCompany.Rows.Count; i++)
{
if (Convert.ToString (dgvCompany.Rows[i].Cells["CompanyLead"].Value) == "1")
{
dgvCompany.Rows[i].Cells["checkBoxColumn"].Value = true;
}
}
Is what I am getting at is that if the grids DataSource is a DataTable AND the CompanyLead column in the data table is of a string type, then simply add the code below to add a bool “Expression” column to the existing table. DataColumn.Expression Property
With this, then ALL the posted code you have shown becomes unnecessary. Something like…
ds.Tables[0].Columns.Add("CheckBoxColumn", typeof(bool), "IIF(CompanyLead = 1, true, false)");
Try to change:
dgvCompany.Rows[i].Cells["checkBoxColumn"].Value = true;
to:
dgvCompany.Rows[i].Cells["checkBoxColumn"].Checked = true;
if the checked not appear in the IDE, try to cast this cell to a checkbox
var test = (Checkbox)dgvCompany.Rows[i].Cells["checkBoxColumn"];
test.Checked = true;

Visibility.Visible not working

I am creating and binding columns/elements into my datagrid.
I created a DataGridTemplateColumnand in that column i'm inserting a TextBox element.
DataGridTemplateColumn columnFeedbackSupplier = new DataGridTemplateColumn();
var dockPanel = new FrameworkElementFactory(typeof(DockPanel));
DataTemplate cellTemplate = new DataTemplate();
FrameworkElementFactory factoryText = new FrameworkElementFactory(typeof(TextBox));
Binding bindText = new Binding("Supplier");
bindText.Mode = BindingMode.TwoWay;
factoryText.SetValue(TextBox.TextProperty, bindText);
//This is not working
factoryText.SetValue(TextBox.VisibilityProperty, Visibility.Visible);
//
cellTemplate.VisualTree = factoryText;
dockPanel.AppendChild(factoryText);
cellTemplate.VisualTree = dockPanel;
Now when I add the following line, it allows me to edit my selected cell, but it also hides my TextBox on my datagrid until I double click on that cell then my element is visible again. Once that cell loses focus, my TextBox is hidden again.
columnFeedbackSupplier.CellEditingTemplate = cellTemplate;
I can use the following line to make my TextBox always visible, but I would not be able to save my edited values into my datagrid.
columnFeedbackSupplier.CellTemplate = cellTemplate;
Is there a way to make my TextBox always visible, while using CellEditingTemplate?

C# DataGridView: Value disappearing in dynamic added column

I am coming across something strange or maybe it is the way it works and its just my lack of understand of how the datagridview works. I am dynamically adding a column when I click on a button
private void btnAssign_Click(object sender, EventArgs e)
{
if (!gvData.Columns.Contains("DepartmentId"))
{
DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
DataGridViewColumn dc = new DataGridViewColumn();
dc = new DataGridViewTextBoxColumn();
dc.HeaderText = "DepartmentId";
dc.Name = "DepartmentId";
dc.CellTemplate = cell;
gvData.Columns.Add(dc);
}
foreach(DataGridViewRow row in gvData.SelectedRows)
{
row.Cells["DepartmentId"].Value = getSelectedValues(clbDept);
}
}
first it checks if the DepartmentId column is in the datagridview (dgv). If not then I create a new Datagridviewtextboxcolumn and sets the appropriate settings. Then adds values to the cells. SO far so good... this where is does weird things... when I sort another column, the data in the 'DepartmentId' column disappears. Data disappears in the columns that I dynamically create. Do i have to use a save method or something?
Instead of adding data directly to datagridview you need to add column to datasource and reassign the datasource to datagridview. This will resolve your sorting issue.

Add buttons to datagridview in windows form

I want to add two buttons onto datagridview. Now they are on the right side. But I want to locate them on the left.
The other thing is I want to add an update event for "Edit" button. Is it
private void Edit_Click(object sender, EventArgs e)
{
}
Form code:
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'qDataSet.Metric' table.
// You can move, or remove it, as needed.
this.metricTableAdapter.Fill(this.qDataSet.Metric);
DataGridViewButtonColumn EditColumn = new DataGridViewButtonColumn();
EditColumn.Text = "Edit";
EditColumn.Name = "Edit";
EditColumn.DataPropertyName = "Edit";
dataGridView1.Columns.Add(EditColumn);
DataGridViewButtonColumn DelColumn = new DataGridViewButtonColumn();
DelColumn.Text = "Delete";
DelColumn.Name = "Delete";
DelColumn.DataPropertyName = "Delete";
dataGridView1.Columns.Add(DelColumn);
}
The image likes:
Thank you.
You seems to have design the datagridview from the designer.
You can use the wizard which allow to edit columns. There you'll add two columns (one for editing and the other for deleting). You can choose where you want to set those columns.
The text of the button is defined using the property "Text", you can also set the property "UseColumnTextForButton".
You can manage easily in the CellContentClickEvent the column and row which is clicked and then do the job.
If you want to manage access right (like allowing someone to editing and someone else to not editing) you can play with show/hide of this column.
See DisplayIndex
The zero-based position of the column as it is displayed in the associated DataGridView, or -1 if the band is not contained within a control.
EditColumn.DisplayIndex = 0;
DelColumn.DisplayIndex = 1;
You can't subscribe to the Edit button events directly.
So, the decision to subscribe to the CellClick event and check which are pressed
And yes, for columns position set DisplayIndex property
DataGridViewButtonColumn ButtonColumn = new DataGridViewButtonColumn();
ButtonColumn.Name = "Print";
ButtonColumn.HeaderText = "Print";
ButtonColumn.FlatStyle = FlatStyle.Popup;
ButtonColumn.DefaultCellStyle.ForeColor = Color.White;
ButtonColumn.DefaultCellStyle.BackColor = Color.CadetBlue;
ButtonColumn.Text = "Print";
ButtonColumn.UseColumnTextForButtonValue = true;
int columnIndex = 12; /*your column index number*/
if (dtGridTicket.Columns["Print"] == null)
{
dtGridTicket.Columns.Insert(columnIndex, ButtonColumn);
}
If you load the page column index hide so you use
dtGridTicket.Columns.RemoveAt(12);

ComboBox data binding

I have a combobox control on form that pull its data (Displays and values) from some datasource. On another side I have table with one row. I want when app is lauching, combobox set selectedvalue or selecteditem to value of one column in above row. And when user has changed combobox it will persist change to row. I have tried to bind SelectedValue to this column, but it doesn't work. Combobox just sets on start to first item. What is problem?
EDIT
This is a Win Forms project.
Here is the binding code:
this.comboBoxCountries = new System.Windows.Forms.ComboBox();
this.countriesBindingSource = new System.Windows.Forms.BindingSource(this.components);
//
// comboBoxCountries
//
this.comboBoxCountries.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.searchCriteriaBindingSource, "Postcode", true));
this.comboBoxCountries.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.searchCriteriaBindingSource, "CountryCode", true));
this.comboBoxCountries.DataSource = this.countriesBindingSource;
this.comboBoxCountries.DisplayMember = "Name";
this.comboBoxCountries.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxCountries.FormattingEnabled = true;
this.comboBoxCountries.Location = new System.Drawing.Point(190, 19);
this.comboBoxCountries.Name = "comboBoxCountries";
this.comboBoxCountries.Size = new System.Drawing.Size(156, 21);
this.comboBoxCountries.TabIndex = 2;
this.comboBoxCountries.ValueMember = "Code";
this.comboBoxCountries.SelectedValueChanged += new System.EventHandler(this.comboBoxCountries_SelectedValueChanged);
//
// countriesBindingSource
//
this.countriesBindingSource.DataMember = "Countries";
this.countriesBindingSource.DataSource = this.dbDataSetCountries;
//
// dbDataSetCountries
//
this.dbDataSetCountries.DataSetName = "dbDataSetCountries";
this.dbDataSetCountries.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
//
// searchCriteriaBindingSource
//
this.searchCriteriaBindingSource.AllowNew = false;
this.searchCriteriaBindingSource.DataMember = "SearchCriteria";
this.searchCriteriaBindingSource.DataSource = this.dbDataSetSearchCriteria;
this.searchCriteriaBindingSource.BindingComplete += new System.Windows.Forms.BindingCompleteEventHandler(this.searchCriteriaBindingSource_BindingComplete);
//
// dbDataSetSearchCriteria
//
this.dbDataSetSearchCriteria.DataSetName = "dbDataSetSearchCriteria";
this.dbDataSetSearchCriteria.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
EDIT2
As I have mentioned in my comment below, I have another textbox that is binded to another DataMember of same binding source and textbox working fine. It's appear with appropriate value. When I change DataMember on same datamember on which I set selectedvalue property of combobox binding it's also show a good result and work properly.
Thanks in advance!
Take a look at the DisplayMember and ValueMember properties of the combobox. You need to tell the ComboBox what member from the datasource to display in the drop down, and what value to give when SelectedValue is requested.
It sounds like your ComboBox is bound to a static list while your rows are not. You might consider using a BindingSource that you set the ComboBox and the DataGridView's DataSource to. That way when the DGV navigates to a new row, the ComboBox will be updated with the value for the new row.
Here is a link to the ComboBox on MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx
I find it out. So for managing with this issue you should remove SelectedValue databinding from visual studio data bound menu and put an appropriate code to add this databinding in some place after filling all bindingsources:
private void MainForm_Load_1(object sender, EventArgs e)
{
this.searchCriteriaTableAdapter1.Fill(this.dbDataSetCountries.SearchCriteria);
this.searchCriteriaTableAdapter.Fill(this.dbDataSetSearchCriteria.SearchCriteria);
comboBoxCountries.DataBindings.Add("SelectedValue", this.dbDataSetCountries.SearchCriteria, "CountryCode");
}

Categories