DevExpress GridControl line numbers - c#

How to make column with row number? Solutions that works with default WPF dataGrid don't work with DevExpress...

You need to add a unboundcolumn to your gridview, you can do this from the designer or from code.
var col = gridView1.Columns.Add();
col.FieldName = "counter";
col.Visible = true;
col.UnboundType = DevExpress.Data.UnboundColumnType.Integer;
gridView1.CustomUnboundColumnData += gridView1_CustomUnboundColumnData;
void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
if (e.IsGetData)
e.Value = e.ListSourceRowIndex+1;
}

set the column caption to "#"
then add this event to the gridView1
private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
{
if (e.Column.Caption == "#")
{
e.DisplayText = (e.RowHandle + 1).ToString();
}
}

Related

ComboBoxCell Value is not valid

I have a datagridview binded to a BindingList and inside this list I have comboboxes binded to a list which is a property of my BindingList, for understanding better:
ListA ---> binded to datagridview
ListA.ListB ---> binded to comboboxes
When I open the form I can corectly set my comboboxes showing the values inside the ListB, but when I add a new item I get an error (value is not valid), here is the code:
private void dataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
((DataGridViewComboBoxColumn)dataGridView.Columns["Names"]).DisplayIndex = 4;
for (int i = 0; i < People.Count; i++)
{
var cell = (DataGridViewComboBoxCell)dataGridView.Rows[i].Cells["Names"];
cell.DataSource = People[i].Names;
cell.Value = People[i].Names[0];
}
}
The code above works great, the problem happens here:
private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (dataGridView.CurrentCell.ColumnIndex != dataGridView.Columns["Names"].Index)
return;
var cell = (DataGridViewComboBoxCell)dataGridViewICAO.CurrentCell;
if (cell.EditedFormattedValue.ToString().Equals(String.Empty)) return;
var regex = new Regex("[a-zA-Z]");
if (!regex.IsMatch(cell.EditedFormattedValue.ToString()))
e.Cancel = true;
else
{
People[cell.RowIndex].Names.Add(cell.EditedFormattedValue.ToString());
cell.Value = People[cell.RowIndex].Names.Last();
People[cell.RowIndex].Names = cell.Value.ToString();
}
}
on the row code cell.Value = People[cell.RowIndex].Names.Last(); I get the exception... Thanks to all!
This is how I set the combobox:
private void AddComboBox()
{
var comboNames = new DataGridViewComboBoxColumn { Name = "cmbNames", HeaderText = "Names" };
dataGridView.Columns.Add(comboNames);
}
private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView.CurrentCell.ColumnIndex == dataGridView.Columns["cmbNames"].Index)
{
var combo = e.Control as ComboBox;
if (combo == null)
return;
combo.DropDownStyle = ComboBoxStyle.DropDown;
}
}

Space between column in RadGridView in silverlight

I am showing columns dynamically in Radgridview using c#.
private void chk_Click(object Sender, RoutedEventArgs e)
{
System.Windows.Controls.CheckBox ChkBox = ((System.Windows.Controls.CheckBox)e.OriginalSource);
string ControlName = ChkBox.Name.Substring(4);
if (ChkBox.IsChecked == true)
{
VReportViewer.GrdReport.Columns[ControlName].IsVisible = true;
}
else
{
VReportViewer.GrdReport.Columns[ControlName].IsVisible = false;
}
return;
}
But when i click check box ..columns are showing.But in Between column i am getting space.
Let me know how i solve this.
..

How to add TextBox in DataGridView at specific column?

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox txtbox = e.Control as TextBox;
if (this.dataGridView1.CurrentCell.ColumnIndex == 0)
{
if (txtbox != null)
{
//
}
}
}
Also I have coded with AutoCompleteStringCollection.
Code is working,
Before edit Column 1, It won't allow autoCompletion for any of other column.
Once edited Column 1, all column will work same as Column one.
Please help me how to fix issue or else any other best way to do that, please share here.
This should work.
private bool firstColEdited = false;
/************************************************************/
var source = new AutoCompleteStringCollection();
String[] stringArray = Array.ConvertAll<DataRow, String>(products.Select(), delegate(DataRow row) { return (String)row["code"]; });
source.AddRange(stringArray);
/************************************************************/
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox txtbox = e.Control as TextBox;
if (this.dataGridView1.CurrentCell.ColumnIndex == 0 || firstColEdited)
{
firstColEdited = true;
txtbox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtbox.AutoCompleteCustomSource = source;
txtbox.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}

how to allow user manual entry in datagridview combobox in c#

I am trying to enter values in a datagridview Combobox. but it does not Allows. What to do?
private void GridStockItemEntry_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewRow row = GridStockItemEntry.CurrentRow;
DataGridViewCell cell = GridStockItemEntry.CurrentCell;
if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
{
if (cell == row.Cells["ItemName"] && Convert.ToString(row.Cells["Type"].Value) == "Raw Material")
{
DataGridViewComboBoxEditingControl cbo = e.Control as DataGridViewComboBoxEditingControl;
cbo.DropDownStyle = ComboBoxStyle.DropDown;
cbo.Validating += new CancelEventHandler(cbo_Validating);
}
}
}
void cbo_Validating(object sender, CancelEventArgs e)
{
DataGridViewComboBoxEditingControl cbo = sender as DataGridViewComboBoxEditingControl;
DataGridView grid = cbo.EditingControlDataGridView;
object value = cbo.Text;
// Add value to list if not there
if (cbo.Items.IndexOf(value) == -1)
{
DataGridViewComboBoxCell cboCol = (DataGridViewComboBoxCell)grid.CurrentCell;
// Must add to both the current combobox as well as the template, to avoid duplicate entries...
cbo.Items.Add(value);
cboCol.Items.Add(value);
grid.CurrentCell.Value = value;
}
}
Maybe, this example is better readable:
private void datagridview_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
DataGridView dgv = (DataGridView)sender;
if(dgv.CurrentCell.ColumnIndex==dgv.Columns["ColumnName"].Index) {
ComboBox cbx = (ComboBox)e.Control;
cbx.DropDownStyle = ComboBoxStyle.DropDown;
cbx.AutoCompleteSource = AutoCompleteSource.ListItems;
cbx.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
}
}
Make sure that EditMode property of the DataGridView is set to EditOnKeystrokeOrF2
Also, verify that ReadOnly property is set to False.

How to host datetimepicker in DataGridView control in C# winforms

private void showdate(DataGridViewCellEventArgs e)
{
dateTimePicker1.Size = vacation_transDataGridView.CurrentCell.Size;
dateTimePicker1.Top = vacation_transDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Top + vacation_transDataGridView.Top;
dateTimePicker1.Left = vacation_transDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Left + vacation_transDataGridView.Left;
if (!(object.Equals(Convert.ToString(vacation_transDataGridView.CurrentCell.Value),"")))
{
dateTimePicker1.Value = Convert.ToDateTime(vacation_transDataGridView.CurrentCell.Value);
//dateTimePicker1.Visible = false;
}
dateTimePicker1.Visible = true;
}
This code in dgv cell_click event
There is an example of exactly this thing on MSDN.
http://msdn.microsoft.com/en-us/library/7tas5c80.aspx
Unfortunately there isn't such a Cell or Column Type you can use (as far as i know). But Microsoft provides an example on how to get a NumericUpDown (that also doesn't exist) into a DataGridView.
Maybe you can adopt this example to get your DateTimePicker into a DataGridView.
private void dgtest_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
dtp = new DateTimePicker();
dgtest.Controls.Add(dtp);
dtp.Format = DateTimePickerFormat.Short;
Rectangle Rectangle = dgtest.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
dtp.Size = new Size(Rectangle.Width, Rectangle.Height);
dtp.Location = new Point(Rectangle.X, Rectangle.Y);
dtp.CloseUp += new EventHandler(dtp_CloseUp);
dtp.TextChanged += new EventHandler(dtp_OnTextChange);
dtp.Visible = true;
}
}
private void dtp_OnTextChange(object sender, EventArgs e)
{
dgtest.CurrentCell.Value = dtp.Text.ToString();
}
void dtp_CloseUp(object sender, EventArgs e)
{
dtp.Visible = false;
}

Categories

Resources