Ik heb een datagrid gemaakt en daaraan edit/cancel/update koppen toegevoegd en een sortknop. Het probleem is nu dat als ik de datagrid sorteer de itemindex niet wordt aangepast. Dit levert problemen op als je de datagrid sorteert en vervolgens een kolom wil wijzigen. Ipv dat de gekozen kolom dan editable wordt, wordt de kolom editable die in eerste instantie op die positie stond. Heeft iemand een oplossing?
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
| private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
LbResult.Text = "BLAAT: ";
BindGrid();
}
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string s = "UPDATE gebruiker SET achternaam = '"+ ((TextBox)e.Item.Cells[2].Controls[0]).Text.Trim()+"', voornaam = '"+ ((TextBox)e.Item.Cells[3].Controls[0]).Text.Trim()+"', voorletters = '"+ ((TextBox)e.Item.Cells[4].Controls[0]).Text.Trim()+"', emailadres = '"+ ((TextBox)e.Item.Cells[5].Controls[0]).Text.Trim()+"', directe_manager = '"+ ((TextBox)e.Item.Cells[6].Controls[0]).Text.Trim()+"', afdeling = '"+ ((TextBox)e.Item.Cells[7].Controls[0]).Text.Trim()+"' WHERE personeelsnummer = '"+DataGrid1.DataKeys[e.Item.ItemIndex]+"'";
sqlUpdateCommand1.CommandText = s;
if(((TextBox)e.Item.Cells[2].Controls[0]).Text.Trim()==""||((TextBox)e.Item.Cells[3].Controls[0]).Text.Trim()==""||((TextBox) e.Item.Cells[4].Controls[0]).Text.Trim()==""||((TextBox)e.Item.Cells[5].Controls[0]).Text.Trim()==""||((TextBox) e.Item.Cells[6].Controls[0]).Text.Trim()==""||((TextBox)e.Item.Cells[7].Controls[0]).Text.Trim()=="")
{
LbResult.Text = "U dient wel alle velden in te vullen";
}
else if(((TextBox)e.Item.Cells[5].Controls[0]).Text.IndexOf("@")==-1||((TextBox)e.Item.Cells[5].Controls[0]).Text.IndexOf(".")==-1)
{
LbResult.Text = "U dient wel een geldig emailadres in te vullen";
}
else
{
try
{
sqlConnection1.Open();
sqlUpdateCommand1.ExecuteNonQuery();
sqlConnection1.Close();
}
catch(System.Data.SqlClient.SqlException se)
{
String fout = se.ToString();
if(fout.IndexOf("column of data type int")!=-1)
{
LbResult.Text = "Mislukt: U dient wel een getal in te voeren!";
}
else
{
LbResult.Text = "Mislukt: "+se.ToString();
}
}
//Switch out of edit mode.
DataGrid1.EditItemIndex = -1;
BindGrid();
}
}
private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
sqlSelectCommand1.CommandText = "SELECT * FROM Gebruiker Order By " + e.SortExpression.ToString();
BindGrid();
}
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
String s = "Delete From Gebruiker where personeelsnummer =" + DataGrid1.DataKeys[e.Item.ItemIndex];
sqlDeleteCommand1.CommandText = s;
LbResult.Text = s;
sqlConnection1.Open();
sqlDeleteCommand1.ExecuteNonQuery();
sqlConnection1.Close();
BindGrid();
} |