I have a screen containing infragistics windows editable grid and winforms button.
On Enter key, the new row gets added in ultrawin grid. Instead i need to fire winforms button event.
Only on tab, it should navigate to next cell and when it reaches last cell, it should navigate to next row.
You can handle UltraGrid's BeforeRowUpdate event and invoke UltraButton's perform click before the new row template is about to be commited.
public partial class Form1 : Form
{
private bool _IsEnterKeyDown = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ultraGrid1.KeyDown += new KeyEventHandler(ultraGrid1_KeyDown);
ultraGrid1.BeforeRowUpdate += new CancelableRowEventHandler(ultraGrid1_BeforeRowUpdate);
ultraGrid1.InitializeLayout += new Infragistics.Win.UltraWinGrid.InitializeLayoutEventHandler(ultraGrid1_InitializeLayout);
ultraGrid1.DataSource = GetDataTable();
}
private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
_IsEnterKeyDown = true;
}
}
private void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
{
if (e.Row.IsAddRow && _IsEnterKeyDown)
{
_IsEnterKeyDown = false;
ultraButton1.PerformClick();
}
}
private void ultraButton1_Click(object sender, EventArgs e)
{
MessageBox.Show("Button click event raised!");
}
private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
{
e.Layout.Override.AllowAddNew = AllowAddNew.TemplateOnBottom;
}
private DataTable GetDataTable(int rows = 10)
{
DataTable table = new DataTable("Table1");
table.Columns.Add("Boolean column", typeof(bool));
table.Columns.Add("Integer column", typeof(int));
table.Columns.Add("DateTime column", typeof(DateTime));
table.Columns.Add("String column", typeof(string));
for (int i = 0; i < rows; i++)
{
DataRow row = table.NewRow();
row["Boolean column"] = i % 2 == 0 ? true : false;
row["Integer column"] = i;
row["String column"] = "text";
row["DateTime column"] = DateTime.Today.AddDays(i);
table.Rows.Add(row);
}
return table;
}
}
Related
I'm trying to put text in a specific cell using drag and drop screenshot of my screen
There is a simplified solution provided here, please check -
https://www.codeproject.com/Questions/101532/How-to-apply-drag-drop-cell-content-in-datagridvie
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
//allow drop
this.dataGridView1.AllowDrop = true;
//data grid view sample data
DataTable dtb = new DataTable();
dtb.Columns.AddRange(new DataColumn[] { new DataColumn(), new DataColumn(), new DataColumn() });
for (int i = 0; i < 30;)
{
dtb.Rows.Add((++i).ToString(), (++i).ToString(), (++i).ToString());
}
dataGridView1.DataSource = dtb;
}
private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
dataGridView1.DoDragDrop(dataGridView1[e.ColumnIndex,e.RowIndex].FormattedValue, DragDropEffects.Copy);
}
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
string cellvalue=e.Data.GetData(typeof(string)) as string;
Point cursorLocation=this.PointToClient(new Point(e.X,e.Y));
System.Windows.Forms.DataGridView.HitTestInfo hittest= dataGridView1.HitTest(cursorLocation.X,cursorLocation.Y);
if (hittest.ColumnIndex != -1
&& hittest.RowIndex != -1)
dataGridView1[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue;
}
private void dataGridView1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
}
I am adding a delete function in my data grid view at the moment.
I add the delete button by code, and I plan to trigger the delete function via Event.
private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//if click is on new row or header row
if (e.RowIndex == dataGridView1.NewRowIndex || e.RowIndex < 0)
return;
//Check if click is on specific column
if (e.ColumnIndex == dataGridView1.Columns["dataGridViewDeleteButton"].Index)
{
System.Console.WriteLine("delete button pressed!");
dataGridView1.Rows.RemoveAt(e.RowIndex);
}
}
I have been tested the function, the debug console actually prints out the delete button pressed!, which means the Event is triggered successfully.
However, I am running into an issue; how to update the underline data source after I removed the cell at specific row?
The data source code is :
DataTable dataTable = new DataTable();
// data loading ...
Here is my test.
public Form1()
{
InitializeComponent();
testdgv.CellClick += testdgv_CellClick;
}
DataTable table = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Description", typeof(string));
table.Rows.Add(1, "KKK", "Des1");
table.Rows.Add(2, "YYY", "Des2");
table.Rows.Add(3, "LLL", "Des3");
table.Rows.Add(4, "EEE", "Des4");
table.Rows.Add(5, "TTT", "Des5");
testdgv.DataSource = table;
}
private void testdgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
// if Description clicked, delete the row
if (e.ColumnIndex == testdgv.Columns["Description"].Index)
{
System.Console.WriteLine("delete button pressed!");
testdgv.Rows.RemoveAt(e.RowIndex);
}
}
// test
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < table.Rows.Count; i++)
{
string ID = table.Rows[i][0].ToString();
Console.WriteLine(ID);
}
}
Test result:
I am doing a project where I need to sort My data grid by priority in descending order after I click block button. Once Block is Clicked the priority in that row changes from whatever the number is to -1 which should move it to the bottom of the grid and push the highest priority value to the top of the grid. It works the first time I click the block button but after that nothing else responds to Any clicks of the buttons Ready or Block. I want to move all block items to the bottom of the list with -1 values and then return all the items to there proper rank in the grid after I click ready. Can someone explain why it only sorts my grid on the first click of the delete button?
namespace Dispatcher_2._0
{
public partial class Form1 : Form
{
DataTable Table = new DataTable();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Table.Columns.Add("PID", typeof(int));
Table.Columns.Add("Status", typeof(string));
Table.Columns.Add("Priority", typeof(int));
Table.Rows.Add(91, "Current Process", 0);
Table.Rows.Add(92, "Ready", 23);
Table.Rows.Add(95, "Ready", 22);
Table.Rows.Add(93, "Ready", 28);
Table.Rows.Add(94, "Ready", 44);
dataGridView1.DataSource = Table;
DataGridViewButtonColumn Btn = new DataGridViewButtonColumn();
DataGridViewButtonColumn Btn1 = new DataGridViewButtonColumn();
DataGridViewButtonColumn Btn2 = new DataGridViewButtonColumn();
Btn.Name = "Ready";
Btn.Text = "Ready";
Btn1.Name = "Block";
Btn1.Text = "Block";
Btn2.Name = "Terminate";
Btn2.Text = "Terminate";
Btn.UseColumnTextForButtonValue = true;
Btn1.UseColumnTextForButtonValue = true;
Btn2.UseColumnTextForButtonValue = true;
dataGridView1.Columns.Add(Btn);
dataGridView1.Columns.Add(Btn1);
dataGridView1.Columns.Add(Btn2);
}
private void button3_Click(object sender, EventArgs e)
{
Table.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count == 0)
{
Table.Rows.Add(91, "Current Process", 0);
Table.Rows.Add(92, "Ready", 23);
Table.Rows.Add(95, "Ready", 22);
Table.Rows.Add(93, "Ready", 28);
Table.Rows.Add(94, "Ready", 44);
dataGridView1.DataSource = Table;
}
else { }
dataGridView1.Refresh();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int selectedRow = dataGridView1.CurrentCell.RowIndex;
string value = dataGridView1.Rows[0].Cells[1].FormattedValue.ToString();
string value1 = dataGridView1.Rows[selectedRow].Cells[selectedRow].FormattedValue.ToString();
if (dataGridView1.Columns[e.ColumnIndex].Name == "Ready")
{
dataGridView1.Rows[selectedRow].Cells[1].Value = "Ready";
}
if (dataGridView1.Columns[e.ColumnIndex].Name == "Block")
{
dataGridView1.Rows[selectedRow].Cells[1].Value = "Blocked";
dataGridView1.Rows[selectedRow].Cells[2].Value = "-1";
DataView Tab = new DataView(Table);
Tab.Sort = "Priority desc";
dataGridView1.DataSource = Tab;
}
if (dataGridView1.Columns[e.ColumnIndex].Name == "Terminate")
{
dataGridView1.Rows.RemoveAt(0);
}
}
private void button1_Click(object sender, EventArgs e)
{
Table.Rows.Add(new Random().Next(50, 100), "Ready", textBox1.Text);
}
}
}
I have always simply filled in my tables without using a data source so I'm not exactly sure how this works. However, I think I see your problem anyway--it sounds like you need to write a custom comparison function to sort the table with. This is necessary any time you have data that must be sorted based on multiple columns.
How can i get multiple selected rows in gridview using c# code & that selected rows i have to display in another form which also have gridview
public partial class WindowForm: Form
{
private DataTable dataTable = new DataTable();
//This will contain all the selected rows.
private List<DataGridViewRow> selectedRows = new List<DataGridViewRow>();
public WindowForm()
{
InitializeComponent();
dataTable .Columns.Add("Column1");
dataTable .Columns.Add("Column2");
dataTable .Columns.Add("Column3");
for (int i = 0; i < 30; i++)
{
dataTable .Rows.Add(i, "Row" + i.ToString(), "Item" + i.ToString());
}
dataGridView1.DataSource = dataTable ;
//This will select full row of a grid
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
//This will allow multi selection
dataGridView1.MultiSelect = true;
dataGridView1.CurrentCellChanged += new EventHandler(dataGridView1_CurrentCellChanged);
dataGridView1.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);
}
void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
PerformSelection(dataGridView1, selectedRows);
}
void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
if (selectedRows.Contains(dataGridView1.CurrentRow))
{
selectedRows.Remove(dataGridView1.CurrentRow);
}
else
{
selectedRows.Add(dataGridView1.CurrentRow);
}
PerformSelection(this.dataGridView1, selectedRows);
}
private void PerformSelection(DataGridView dgv, List<DataGridViewRow> selectedRowsCollection)
{
foreach (DataGridViewRow dgvRow in dgv.Rows)
{
if (selectedRowsCollection.Contains(dgvRow))
{
dgvRow.Selected = true;
}
else
{
dgvRow.Selected = false;
}
}
}
}
I have a grideview and 2 buttons. I need to only show the buttons when the gridview has a selected item. My code looks like this:
protected void Page_Load(object sender, EventArgs e)
{
btactivate.Visible = false;
btdeactivate.Visible = false;
//Show Activate and Deactivate Buttons only if an item in the gridview is selected
if (GridView1.SelectedIndex != -1)
{
btactivate.Visible = true;
btdeactivate.Visible = true;
}
else
{
btactivate.Visible = false;
btdeactivate.Visible = false;
}
}
But the problem I have now is that only when I select the second time a item in the gridview the buttons show up. I need to have the the buttons show when I select the first time. I have tried changing the selected index to "-0" but that shows the buttons all the time (even when I dont have something selected). Can anyone please help?
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Col1");
dt.Columns.Add("Col2");
dt.Columns.Add("Col3");
for (int i = 0; i < 20; i++)
{
DataRow dr = dt.NewRow();
dr["Col1"] = string.Format("Row{0}Col1", i + 1);
dr["Col2"] = string.Format("Row{0}Col2", i + 1);
dr["Col3"] = string.Format("Row{0}Col3", i + 1);
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
SetButtonState();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
SetButtonState();
}
private void SetButtonState()
{
btactivate.Visible = GridView1.SelectedIndex > -1;
btdeactivate.Visible = GridView1.SelectedIndex > -1;
}