I'm trying to add AutoCompleteSource in datagridViewtextBoxColumn. I'm trying two methods.In the first one i'm directly adding AutoCompleteSource to datagridViewColumn. And in the second i created a textBox on the desired cell and added AutocompleteCustome Source. But none of this working with no exception.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewCell cel = dataGridView1.CurrentCell;
DataGridViewRow row = dataGridView1.CurrentRow;
if (e.Control.GetType() == typeof(DataGridViewTextBoxEditingControl))
{
if (cel == row.Cells[1])
{
DataGridViewTextBoxEditingControl t = e.Control as DataGridViewTextBoxEditingControl;
AutoCompleteStringCollection ccl = new AutoCompleteStringCollection();
foreach (DataRow rw in bowoniDataSet17.item.Rows)
{
ccl.Add(rw.ToString());
}
t.AutoCompleteSource = AutoCompleteSource.CustomSource;
t.AutoCompleteCustomSource = ccl;
t.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
}
}
}
TextBox tb = new TextBox();
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
DataGridViewRow row = dataGridView1.CurrentRow;
DataGridViewCell cel = dataGridView1.CurrentCell;
Rectangle rect=dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex,dataGridView1.CurrentCell.RowIndex,true);
tb.Size = new Size(rect.Width, rect.Height);
tb.Location = new Point(rect.X, rect.Y);
tb.TextAlignChanged += new EventHandler(tbtx_OnTextChanged);
if (cel == row.Cells[1])
{
AutoCompleteStringCollection ccl = new AutoCompleteStringCollection();
foreach (DataRow rw in bowoniDataSet17.item.Rows)
{
ccl.Add(rw.ToString());
}
tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
tb.AutoCompleteCustomSource = ccl;
tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
}
}
private void tbtx_OnTextChanged(object sender, EventArgs e)
{
dataGridView1.CurrentCell.Value = tb.Text;
}
Now i changed it to DataGridViewComboxColumn.Working fine.
Related
I am developing a project. In this project, the rows I selected from datagridview1 should be added to the child table and deleted from there. Later, I want to be able to add the rows that I don't want to pull from datagridview2 back to datagridview1. How do I do this?
Here is my codes:
private void button1_Click(object sender, EventArgs e)
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM AçıkKalemler", con);
sda.Fill(dt);
dataGridView1.Rows.Clear();
foreach (DataRow item in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = false; // Select butonlarının seçili olmadan gelmesini sağlar.
dataGridView1.Rows[n].Cells[1].Value = item["MÜŞTERİ"].ToString();
dataGridView1.Rows[n].Cells[2].Value = item["SIPARISNUMARASI"].ToString();
dataGridView1.Rows[n].Cells[3].Value = item["SATISNUMARASI"].ToString();
dataGridView1.Rows[n].Cells[4].Value = item["KALEMTANIMI"].ToString();
dataGridView1.Rows[n].Cells[5].Value = item["PLANLANANTESLİMTARİHİ"].ToString();
dataGridView1.Rows[n].Cells[6].Value = item["SIPARISMIKTARI"].ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in dataGridView1.Rows)
{
if ((bool)item.Cells[0].Value == true)
{
int n = dataGridView2.Rows.Add();
dataGridView2.Rows[n].Cells[0].Value = false;
dataGridView2.Rows[n].Cells[1].Value = item.Cells[1].Value.ToString();
dataGridView2.Rows[n].Cells[2].Value = item.Cells[2].Value.ToString();
dataGridView2.Rows[n].Cells[3].Value = item.Cells[3].Value.ToString();
dataGridView2.Rows[n].Cells[4].Value = item.Cells[4].Value.ToString();
dataGridView2.Rows[n].Cells[5].Value = item.Cells[5].Value.ToString();
dataGridView2.Rows[n].Cells[6].Value = item.Cells[6].Value.ToString();
}
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if ((bool)dataGridView1.SelectedRows[0].Cells[0].Value == false)
{
dataGridView1.SelectedRows[0].Cells[0].Value = true;
}
else
{
dataGridView1.SelectedRows[0].Cells[0].Value = false;
}
}
private void button3_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in dataGridView2.Rows)
{
if ((bool)item.Cells[0].Value == true)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = false;
dataGridView1.Rows[n].Cells[1].Value = item.Cells[1].Value.ToString();
dataGridView1.Rows[n].Cells[2].Value = item.Cells[2].Value.ToString();
dataGridView1.Rows[n].Cells[3].Value = item.Cells[3].Value.ToString();
dataGridView1.Rows[n].Cells[4].Value = item.Cells[4].Value.ToString();
dataGridView1.Rows[n].Cells[5].Value = item.Cells[5].Value.ToString();
}
}
}
private void dataGridView2_MouseClick(object sender, MouseEventArgs e)
{
if ((bool)dataGridView2.SelectedRows[0].Cells[0].Value == false)
{
dataGridView2.SelectedRows[0].Cells[0].Value = true;
}
else
{
dataGridView2.SelectedRows[0].Cells[0].Value = false;
}
}
Button3 should do this.
Please help me!!
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;
}
}
I have a dropdown which is bound to a database. On its index change there is a function that add some button in a panel based upon selected value.
I am reading those button in page_init event but still I get null values, i.e. event bound with the button never fires.
Here is my code and dropdownlist1 is the dropdown that is adding dynamic button.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
colorgroupsTableAdapters.master_color_groupTableAdapter ta
= new colorgroupsTableAdapters.master_color_groupTableAdapter();
DataTable dt = ta.GetData();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = dt.Columns[1].ToString();
DropDownList1.DataValueField = dt.Columns[0].ToString();
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("Select One", "0"));
}
}
protected void Page_Init(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
bindcolors();
}
}
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
}
protected void DropDownList2_DataBound(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex < 1)
{
DropDownList2.Items.Clear();
}
DropDownList2.Items.Insert(0, new ListItem("Select One", "0"));
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ViewState["dd"] = DropDownList1.SelectedIndex;
bindcolors();
}
void bindcolors()
{
if (DropDownList1.SelectedIndex > 0)
{
addcolorgroupsTableAdapters.groupavailablecolorTableAdapter ta
= new addcolorgroupsTableAdapters.groupavailablecolorTableAdapter();
DataTable dt = ta.GetData(int.Parse(DropDownList1.SelectedValue));
HtmlTable ht = new HtmlTable();
ht.Width = "90%";
ht.Border = 1;
for (int i = 0; i < dt.Rows.Count; i++)
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell tc1 = new HtmlTableCell();
HtmlTableCell tc2 = new HtmlTableCell();
HtmlTableCell tc3 = new HtmlTableCell();
object[] ob = dt.Rows[i].ItemArray;
tc1.InnerHtml = ob[0].ToString();
tc2.InnerHtml = ob[1].ToString();
tc2.BgColor = "#" + ob[1].ToString();
Button b = new Button();
b.Text = "Remove";
b.CommandArgument = ob[0].ToString();
AjaxControlToolkit.ConfirmButtonExtender cb
= new AjaxControlToolkit.ConfirmButtonExtender();
cb.ConfirmText = "Are You Sure To Delete This Color From The Group?";
b.ID = "Bo" + ob[0].ToString();
b.EnableViewState = true;
b.Click += new EventHandler(b_Click);
cb.TargetControlID = "Bo" + ob[0].ToString();
tc3.Controls.Add(b);
tc3.Controls.Add(cb);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
tr.Cells.Add(tc3);
ht.Rows.Add(tr);
}
Panel1.Controls.Add(ht);
}
}
void b_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
int grp = int.Parse(DropDownList1.SelectedValue);
int clid = int.Parse(b.CommandArgument);
addcolorgroupsTableAdapters.QueriesTableAdapter ta
= new addcolorgroupsTableAdapters.QueriesTableAdapter();
ta.DeleteQuery_group_color(grp, clid);
DropDownList2.DataBind();
bindcolors();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex > 0 && DropDownList2.SelectedIndex > 0)
{
int grp = int.Parse(DropDownList1.SelectedValue);
int clid = int.Parse(DropDownList2.SelectedValue);
addcolorgroupsTableAdapters.QueriesTableAdapter ta
= new addcolorgroupsTableAdapters.QueriesTableAdapter();
ta.Insert_into_group_color(grp, clid);
DropDownList2.DataBind();
bindcolors();
}
}
Please tell what I am doing wrong?
I think the problem is the check for SelectedIndex > 0 in bindControls. The reason is that ViewState is evaluated between the Init and Load so the value for the SelectedIndex property is not set yet (and therefore = -1).
You could try a different approach: use a Repeater control that is databound to the results of the database query. This way, the general structure can be defined in the Repeater and its ItemTemplate. Also the EventHandlers are registered in the ItemTemplate.
You can reset the DataSource of the Repeater whenever the SelectedIndex of the Combobox changes and do not need to recreate the controls dynamically in init.
Your code should be much shorter and behave more deterministic.
This is what I have coded:
private void DeleteButton_Click(object sender, EventArgs e)
{
//Add column for status
DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
imageColumn.Name = "ImageColumn";
imageColumn.HeaderText = "Status";
FilesDataGridView.Columns.Add(imageColumn);
FilesDataGridView.Columns[2].Width = 45;
foreach (DataGridViewRow dr in FilesDataGridView.Rows)
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dr.Cells["ChkColumn"];
DataGridViewImageCell cell = dr.Cells[2] as DataGridViewImageCell;
if (checkCell.Selected == true)
{
//need code to display the Image for this column here..
}
else
{
//need code to display the Image for this column here..
}
}
}
i have this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Style.Add("text-align", "center");
}
i want to make the header of this cell invisible.. how can i do this by just knowing the cell value...
it does not find the header... i am creating the header at runtime... like this
GridView Grid = new GridView();
Grid.RowDataBound += Grid_RowDataBound;
Grid.ID = machGrps[j].ToString();
//Grid.AutoGenerateColumns = false;
Grid.AllowSorting = false;
Grid.CellSpacing = 2;
Grid.ForeColor = System.Drawing.Color.White;
Grid.GridLines = GridLines.None;
Grid.Width = Unit.Percentage(100);
Grid.Style.Add(HtmlTextWriterStyle.Overflow, "Scroll");
Grid.ShowHeader = true;
DataTable taskTable = new DataTable("TaskList7");
taskTable.Columns.Add("MachineID");
DataRow tableRow = taskTable.NewRow();
tableRow["MachineID"] = machID[i];
taskTable.Rows.Add(tableRow);
Grid.DataSource = taskTable;
Grid.DataBind();
protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Visible = false;
}
}
Sorry for VB.Net :
Select Case e.Row.RowType
Case DataControlRowType.Header
e.Row.Cells(0).Visible = False
End Select
Edit: C#
if (e.Row.RowType == DataControlRowType.Header){
e.Row.Cells(0).Visible = False;
}