Deleting Checked rows from gridview in asp.net - asp.net-4.0

I have a gridview which is binded to the datasource.In that gridview i have one column of checkboxes.I want to delete the rows of gridview by checking the checkbox of particular rows,Finally i have delete button outside the gridview which deletes the checked rows from the gridview.
problem is even though am checking checkboxes of rows..but it's showing checked=false...so none of the rows am able t delete..i tried all the possibilities but its not working
Please give me solution ASAP
this is my code
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkDelete = (CheckBox)row.Cells[5].FindControl("chkDelete");
if (chkDelete != null)
{
if (chkDelete.Checked)//It Showing checked=false
{
//delete code goes here//

Check you page load event, grid view may be rebinding again on each Postbacks. If you found such scenario, make following changes in your code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//logic on page load
}
}

Related

CustomListView in windows mobile 6.5.3

I am using http://christian-helle.blogspot.in/2011/01/multi-platform-mobile-development_19.html for creating a custom list view.
In the existing sample it is not possible to pre set the selectedIndex of list view.
I have made few changes to the sample code and I am able to set the index and highlight it. but the problem is I am not able to set the scroll position to the highlighted item.
I have tried to set scrollBar.Value = itemindex, but it is not reflecting on the custom list view.
The simplest solution would be ListView.EnsureVisible.
private ListView listView1;
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (-1 < listView1.SelectedIndex) {
listView1.EnsureVisible(listView1.SelectedIndex);
}
}

Datagridview checkbox column's value

I have a DataGridView with a checkbox column. I want to capture the value of the checkbox immediately when user changes it by clicking. I tried several events (CellValueChanged, CellClicked, CurrentCellDirtyStateChanged etc.) but nothing worked.
This is my code:
If dgvIDsTBC.CurrentRow.Cells(2).Value = True Then
MsgBox("True")
End If
Please help
Hope this helps
void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 3)
MessageBox.Show(dataGridView1[e.ColumnIndex, e.RowIndex].FormattedValue.ToString());
}
This i presume you would have done, now the catch is unless you move out of the cell the grid considers you are still editing ,So Add this part
void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == 3)
dataGridView1.EndEdit();
}
This should be fine for you, 3 is the checkbox column you intend it to work for

SPContext.Current.FormContext.OnSaveHandler not firing when SPControlMode is New

I've got a web part that I'm using to add some custom controls to the New, Edit and Display forms for a SharePoint ListItem. I added a handler for SPContext.Current.FormContext.OnSaveHandler to update the item. I add my web part to the Edit and New forms using SharePoint Designer and hide (set IsVisible=False) for the DataFormWebPart that's put in by default. Everything works fine when editing an item. My OnSaveHandler function is called and I update the SPListItem. The problem is with a new item. The OnSaveHandler function is not called unless I have the DataWebFormPart visible. I make no other changes to the web form but toggle the visibility of the DataFormWebPart. Any ideas what I'm doing wrong?
if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit ||
SPContext.Current.FormContext.FormMode == SPControlMode.New)
{
SPContext.Current.FormContext.OnSaveHandler += FormContext_OnSave;
}
....
protected void FormContext_OnSave(object sender, EventArgs e)
{
//update the list item
}

Unable to perform Up-down operation when wpf datagrid is sorted

HI,
I have a datagrid (from wpf toolkit) which is binded to an observable collection . There are two buttons "Up" and "Down" which moves the selected row accordingly.
For eg: If there are 10 items in the datagrid and if I select the 4th item and press "Up" arrow ,then the 4th item will be moved to 3rd item and previous 3rd item will be 4th item. ie swapping with previous item. Similarly "Down" arrow the other way.
private void BtnUpArrowClick(object sender, RoutedEventArgs e)
{
try
{
btnDownArrow.IsEnabled = true;
currentRow = grdSeqData.SelectedIndex;
if (currentRow == 0)
{
btnUpArrow.IsEnabled = false;
}
else
{
upRow = currentRow - 1;
if (upRow >= 0)
{
sequenceDataList.Move(currentRow, upRow);
currentRow = upRow;
}
else
{
btnUpArrow.IsEnabled = false;
}
}
}
catch (Exception ex)
{
CatchExeption(LanguageHandler.GetCurrentCultureText("EventUpArrow_Exception"), ex);
}
}
The above code work fine when the datagrid is not sorted. When i sort the datagrid via header click, and then do the "Up" or "Down" operation nothing is reflected in the datagrid.
The view is getting sorted where as my binded collection remains the same. So i tried to move the collection from view but in vain.
ICollectionView view = CollectionViewSource.GetDefaultView(grdSeqData.ItemsSource) as ListCollectionView;
view.MoveCurrentToNext();
I think when the datagrid sortdescription is applied, then the view will not move accordingly. Please help.

Forcing Default button on a gridview

I'm using gridview with templates to show and edit some information from a sql database.
When I edit and change the data in that row and then click enter it automatically presses the highest on page button which uses submit to server set to true which means it'll try to delete instead of update.
I've have tried setting a panel round the gridview and setting the panel's default button to the "updatebutton" but it won't allow that because it can't 'see' the buttons.
I had a similar problem and I found a very simple workaround:
Place a button below the GridView and make it invisible by CSS (i.e. position: relative; left: -2000px)
Create a Panel around the GridView and give it as DefaultButton the ID of the button we just created.
Write the following line of code for the click-event of the button:
myGridView.UpdateRow(myGridView.EditIndex, false);
Whenever you press enter in the GridView now, the edited row will be confirmed.
You need to precess KeyDown or KeyPress event of the grid, and check if pressed key if Keys.Enter :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1_Click(this, EventArgs.Empty);
}
}
private void button1_Click(object sender, EventArgs e)
{
// Your logic here
}
}