C# insert custom value selected item from checkedlistbox to dataviewgrid - c#

Im working in a lab for covid. We are sending result via excel. I made a template for easy edit excel file. I searched too much but i cant find solution for this request.
in this picture after selected items and push "tamamlandı" button, i want to change dataviewgrid second column change custom data
for example: if selected 1,5,13,48 in checkedlistbox, change 1,5,13,48 rows second column data to "POZ"

You can get all checked items via CheckedItems property. Then you can use the foreach statement to traverse it.
private void btntamamlandı_Click(object sender, EventArgs e)
{
foreach(var i in checkedListBox1.CheckedItems)
{
// get the checked item value
// rowindex starts from 0, so need to subtract 1
int rowindex = Convert.ToInt32(i.ToString()) - 1;
dataGridView1.Rows[rowindex].Cells[1].Value = "POZ";
}
}
Update: delete selected rows
private void btnDelete_Click(object sender, EventArgs e)
{
// store checkedListBox1 selected index
List<int> indexs = new List<int>();
foreach (var i in checkedListBox1.CheckedItems)
{
indexs.Add(Convert.ToInt32(i.ToString()) - 1);
}
// selected row count
int count = indexs.Count;
for (int index = 0; index < count; index++)
{
dataGridView1.Rows.RemoveAt(indexs[0] - index);
indexs.RemoveAt(0); // remove the first item
}
}

Related

How to get the selected row index from an asp.net gridview row

I have a gridview with no datakeys. When I click the row edit button, I'm taken to the rowediting method. I've tried several ways to get the row index, but no luck.
protected void gvwCustomerLocation_RowEditing(object sender, GridViewEditEventArgs e)
{
var index1 = e.NewEditIndex; //gives me an incorrect index number
//The SelectedIndex always equals -1
for (int i = 0; i < gvwCustomerLocation.Rows.Count; i++)
{
if (gvwCustomerLocation.SelectedIndex == 1)
{
}
}
int index = gvwCustomerLocation.EditIndex; //The EditIndex = -1
GridViewRow row = gvwCustomerLocation.SelectedRow; //The SelectedRow = null
var test = row.RowIndex.ToString();
}
How can I get the selected row index?
I eventually gave up on getting a row index from my gridview and decided to make a primary key in my sql server result set to use as my key in the front end.

C# DataGridView reset row new file

I open a CSV File in my DataGridView. When i click on button "Down" the next row is selected.
Problem: when I open a new CSV and click "Down", automatically the selection jumps to the row number of the last selected number of the old CSV.
Example: I select row 11 and open a new file. Row 1 is selected until I press "Down". Instead of row 2, row 11 is selected.
private void btn_down_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count != 0)
{
selectedRow++;
if (selectedRow > dataGridView1.RowCount - 1)
{
selectedRow = 0;
port.Write("...");
}
dataGridView1.Rows[selectedRow].Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;
}
}
You should not use an internal counter to store the selected row since the selection could be changed by other components (in your case by changing the data source). Just make use of the dataGridView1.SelectedRows to get the currently selected row. Based on this row select the next one. Here is a simple implementation:
private void btn_down_Click(object sender, EventArgs e)
{
//Make sure only one row is selected
if (dataGridView1.SelectedRows.Count == 1)
{
//Get the index of the currently selected row
int selectedIndex = dataGridView1.Rows.IndexOf(dataGridView1.SelectedRows[0]);
//Increase the index and select the next row if available
selectedIndex++;
if (selectedIndex < dataGridView1.Rows.Count)
{
dataGridView1.SelectedRows[0].Selected = false;
dataGridView1.Rows[selectedIndex].Selected = true;
}
}
}

How do I add checked item(s) in my checkedlistbox to a DataGridView?

I have a dropdownlist that selects category list from Database and display the product name onto the checkedlistbox. But how do I grabs the checked items from the checkedlistbox to the DataGridView? This is how my design looks like:
Also, how do I make every medication that has been added to the Gridview has a default value of 1 Quantity?
Add this code to your Add button click event:
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
dataGridView1.Rows.Add(checkedListBox1.Items[i], "1");
}
}
}
for (int i = 0; i <= checkedListBox1.SelectedItems.Count - 1; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0].Value = checkedListBox1.SelectedItem.ToString();
}

How to assign a ToolTip for the first column data in the GridView using a ListBox

I have a ListBox with number of items matching the number of rows in the GridView. Using this ListBox I want to display a tooltip for each row only to the first column data in the GridView. I have bound the GridView in the front end.
The code I have tried is giving Index of out range error :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i <= GridView1.Rows.Count; i++)
{
String ProCol = GridView1.Rows[i].Cells[0].ToString();
if (ProCol.Length != 0)
{
e.Row.Cells[0].ToolTip = ListBox1.Items[i].ToString().Trim();
}
}
}
}
It fails in the last iteration of your for cycle, since the index is zero based (first item has 0 index, last item has count-1 index). Replace
i <= GridView1.Rows.Count
with
i < GridView1.Rows.Count
Try moving your code to the DataBound event on the GridView instead of doing the work in RowDataBound. That way, it will only run once when all the rows have been bound and the GridView.Rows collection has been initialized.
Also, you should do what Michal Klouda wrote in his answer regarding <= an <.
And you must make sure that the ListBox1 has been databound before you bind the GridView.
Sample code that should work as long as you bind the ListBox befor the GridView:
protected void GridView1_DataBound(object sender, EventArgs e)
{
var gv = (GridView)sender;
for (int i = 0; i < gv.Rows.Count; i++)
{
var oneRow = gv.Rows[i];
String ProCol = oneRow.Cells[0].ToString();
if (ProCol.Length != 0)
{
oneRow.Cells[0].ToolTip = ListBox1.Items[i].ToString().Trim();
}
}
}

Loading, Updating and displaying Generic List Class

Goal in mind, the concept is kind of like a shopping cart, so as they add items to list(Detail) it keeps the items they are adding in memory.
This works when ever I first load the list(grid) and add more rows. But if I set the first row and set the item and price and then decide to add 3 more rows then
the info I had added gets deleted instead of keeping its values and just load more lines to the list which would repopulate the gridview.
In the past I have done this with datatables but I want to be able to move from that and use this List Class
Also I have it set as viewstate so I can use it through out my page.
private ListArDocumentdetail Detail
{
get
{
ListArDocumentdetail _detail = new ListArDocumentdetail();
if (ViewState["Detail"] != null)
{
_detail = (ListArDocumentdetail)ViewState["Detail"];
}
return _detail;
}
set
{
ViewState["Detail"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
//creates 2 rows to start off
CreateRows(2);
}
public void CreateRows(int rowstoadd)
{
int newtotalrows = Detail.Count + rowstoadd - 1;
for (int i = Detail.Count; i <= newtotalrows; i++)
{
ArDocumentdetail detail = new ArDocumentdetail();
detail.Lineid = i;
detail.Itemid = 0;
detail.Quantity = 1;
if (Detail.Count > 0)
Detail.Insert(Detail.Count, detail);
else
Detail.Add(detail);
Detail = Detail;
}
gvInvoiceDetail.DataSource = Detail;
gvInvoiceDetail.DataBind();
GridViewRow row = gvInvoiceDetail.Rows[gvInvoiceDetail.Rows.Count - 1];
ImageButton btnAdd = (ImageButton)row.FindControl("btnAdd");
btnAdd.Visible = true;
}
protected void ibAdd_Click(object sender, ImageClickEventArgs e)
{
//user can type in how many rows they want to add on to current amount of rows
//so since grid starts off at 2 and they type 3 the grid refreshes with 5 rows.
CreateRows(Convert.ToInt32(txtRows.Text));
}
protected void UpdateRow(object sender, EventArgs e)
{
ImageButton btnUpdate = sender as ImageButton;
GridViewRow row = btnUpdate.NamingContainer as GridViewRow;
TextBox txtPrice = (TextBox)row.FindControl("txtPrice");
TextBox txtQuantity = (TextBox)row.FindControl("txtQuantity");
DropDownList ddlDescription = (DropDownList)row.FindControl("ddlDescription");
int index = Detail.FindIndex(f => f.Lineid == row.RowIndex);
Detail[index].Itemid = Convert.ToInt32(ddlDescription.SelectedValue);
Detail[index].Price = Convert.ToDecimal(txtPrice.Text);
Detail[index].Subtotal = Convert.ToDecimal(Detail[index].Price * Convert.ToInt32(txtQuantity.Text));
}
I can suggest you the logic:
Push a list into viewstate say Viewstate["List"],
Let a user chose an item. Then List list = (List)Viewstate["List"];
Add the selected item to List list. i.e. list.Add(item);
Now push the item back to viewstate. Viewstate["list"] = list;
Bind it to grid or display it on page. Whatever you want.

Categories

Resources