How to display the selected row from listview to textBox?
This is how I do int dataGridView:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].ReadOnly = true;
if (dataGridView1.SelectedRows.Count != 0)
{
DataGridViewRow row = this.dataGridView1.SelectedRows[0];
EmpIDtextBox.Text = row.Cells["EmpID"].Value.ToString();
EmpNametextBox.Text = row.Cells["EmpName"].Value.ToString();
}
}
I tried this:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
ListViewItem item = listView1.SelectedItems[0];
if (item != null)
{
EmpIDtextBox.Text = item.SubItems[0].Text;
EmpNametextBox.Text = item.SubItems[1].Text;
}
}
You may want to check if there is a SelectedItem first. When the selection changed, ListView would actually unselect the old item then select the new item, hence triggering listView1_SelectedIndexChanged twice. Other than that, your code should work:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
ListViewItem item = listView1.SelectedItems[0];
EmpIDtextBox.Text = item.SubItems[0].Text;
EmpNametextBox.Text = item.SubItems[1].Text;
}
else
{
EmpIDtextBox.Text = string.Empty;
EmpNametextBox.Text = string.Empty;
}
}
// select row listview check in c#
foreach (ListViewItem itemRow in taskShowListView.Items)
{
if (itemRow.Items[0].Checked == true)
{
int taskId = Convert.ToInt32(itemRow.SubItems[0].Text);
string taskDate = itemRow.SubItems[1].ToString();
string taskDescription = itemRow.SubItems[2].ToString();
}
}
Related
after clicking my value and pressing my OK_button I cant get the Value out of the listView to save it somewhere else. I cant use listView1.FindItemWithText because I don't have a text to search for.. Idk how to look for the clicked value after I pressed the OK_button
//Create dummy data to display
myData = new string[dataListSize];
for (int i = 0; i < dataListSize; i++)
{
myData[i] = String.Format("{0}", i);
}
}
private void listView1_SearchForVirtualItem(object sender, SearchForVirtualItemEventArgs e)
{
e.Index = Array.FindIndex(myData, s => s == textBox1.Text.ToString());
}
private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
e.Item = new ListViewItem(myData[e.ItemIndex]);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
String MyString = textBox1.Text.ToString();
ListViewItem lvi = listView1.FindItemWithText(MyString.TrimEnd());
//Select the item found and scroll it into view.
if (lvi != null)
{
listView1.SelectedIndices.Clear();
listView1.SelectedIndices.Add(lvi.Index);
listView1.EnsureVisible(lvi.Index);
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e){ }
private void OK_button_Click(object sender, EventArgs e)
{
try
{
// OK -> Daten übernehmen
int iCount = this.listView1.SelectedIndices.Count;
if (iCount != 1)
{
MessageBox.Show("Value is empty");
return;
}
DialogResult = DialogResult.OK;
Close();
}
catch (Exception)
{
//WriteProtokoll(ex.ToString(), 0);
Close();
}
}
I found out that I can get my value with:
string txt = listView1.FocusedItem.Text;
So i wanna delete/remove selected datagridview after i pass to another datagridview
private void BtnRight_Click(object sender, EventArgs e)
{
foreach(DataGridViewRow item in dgAvailabe.Rows)
{
DataGridViewCheckBoxCell chkchecking = item.Cells[0] as DataGridViewCheckBoxCell;
if (Convert.ToBoolean(chkchecking.Value) == true)
{
int n = dgSelected.Rows.Add();
dgSelected.Rows[n].Cells[0].Value = item.Cells[1].Value.ToString();
dgSelected.Rows[n].Cells[1].Value = item.Cells[2].Value.ToString();
dgSelected.Rows[n].Cells[2].Value = item.Cells[3].Value.ToString();
dgSelected.Rows[n].Cells[3].Value = item.Cells[4].Value.ToString();
dgSelected.Rows[n].Cells[4].Value = item.Cells[5].Value.ToString();
}
}
}
I have a datagrid which was declated in xaml called "DataGridUsers" I want to delete the values selected but how do I do that?
Here are my codes
private void BtnDelete_Click(object sender, RoutedEventArgs e)
{
if (DataGridUsers.SelectedItem == null)
{
MessageBox.Show("There is no selected rows!");// show a message here to inform
}
else
{
foreach (DataGrid item in DataGridUsers.SelectedItems)
{
DataGridUsers.Items.Remove(item);
}
}
}
Use SelectedIndex
while (DataGridUsers.SelectedItems.Count > 0){
DataGridUsers.Items.RemoveAt(DataGridUsers.SelectedIndex);
}
Try this:
private void BtnDelete_Click(object sender, RoutedEventArgs e)
{
if (DataGridUsers.SelectedItem == null)
{
MessageBox.Show("There is no selected rows!");// show a message here to inform
}
else
{
DataView dataView = DataGridUsers.ItemsSource as DataView;
if (dataView != null)
{
for (int i = DataGridUsers.SelectedItems.Count - 1; i >= 0; i--)
{
DataRowView drv = DataGridUsers.SelectedItems[i] as DataRowView;
if (drv != null)
{
dataView.Table.Rows.Remove(drv.Row);
}
}
}
}
}
I have a Gridview :
Here when i check one client name's row, all other clients other than that client should be disabled. For example, if i check on 1st row, all client the rows other than having client name Surbhi1 Texttile mills Pvt. Ltd. should be disabled.
This is what i have tried:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
try
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
int selected = Convert.ToInt32(dataGridView1.CurrentRow.Cells["clientid"].Value);
bool isSelected = Convert.ToBoolean(row.Cells["IsChecked"].Value);
if (isSelected)
{
if (Convert.ToInt32(dataGridView1.CurrentRow.Cells["clientid"].Value) == selected)
{
dataGridView1.Rows[0].Cells[0].ReadOnly = true;
}
}
}
}
}
Please provide me suggestions for this.
I made an example with my testdb, If you are using datagrid's events, so you can use datagrid's cell event args.
Here,
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells["checkBoxColumn"].Selected)
{
var clientItem = dataGridView1.Rows[e.RowIndex].Cells["SirName"].Value.ToString();
foreach (DataGridViewRow item in dataGridView1.Rows)
{
if (item.Cells["SirName"].Value.ToString() != clientItem)
{
item.Cells["SirName"].ReadOnly = true;
item.Cells["SirName"].Style.BackColor = Color.Green;
}
}
}
}
and the output was;
So in your situation the code should be;
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells["IsChecked"].Selected)
{
var clientItem = dataGridView1.Rows[e.RowIndex].Cells["clientid"].Value.ToString();
foreach (DataGridViewRow item in dataGridView1.Rows)
{
if (item.Cells["clientid"].Value.ToString() != clientItem)
{
item.Cells["clientid"].ReadOnly = true;
item.Cells["clientid"].Style.BackColor = Color.Green;
}
}
}
}
Hope helps,(Has given backcolor to show better, you can delete.)
I have a comoboBox that is binded to sql database, and I added a default text at index 0 like this
string s = "< -------------Select an application ----------->";
applicationComboBox.Items.Insert(0, s);
applicationComboBox.SelectedIndex = 0;
I am wondering if there is a way to disable my button if the the string s at index 0 is select? In my comboBox, I binded the data with the while(SQLReader.Read()) method instead of using ValueMember and `DisplayMember
Here is what I tried but no luck
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
for (int i = 1; i < applicationComboBox.Items.Count; i++)
{
string value = applicationComboBox.GetItemText(applicationComboBox.Items[0]);
string s = "<------------- Select an application ----------->";
if (value == s)
{
exportButton.Enabled = false;
MessageBox.Show(value); //nothing happen
this.teacherCheckListBox.DataSource = null;
teacherCheckListBox.Items.Clear();
}
else
{
exportButton.Enabled = true;
}
}
}
}
Use SelectedIndex property to know which item is selected and disable the button if it is first item.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
{
exportButton.Enabled = false;
}
}