





用GridView读取数据都没问题了,很奇怪的是当我用更新时,RowUpdating事件中获取控件值不成功,取到的是编辑以前的值,这样的话还怎么更新呀。怪了,弄一天了,没找到原因,各种方法都试过,慢慢弄了,代码如下:
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
using System.Data.OleDb;
12
13
public partial class GridView : System.Web.UI.Page
14
{
15
private string strConn = ConfigurationManager.ConnectionStrings["CONNECTACCESS"].ConnectionString;
16
protected void Page_Load(object sender, EventArgs e)
17
{
18
BindGridView();
19
}
20
protected void myGrid_SelectedIndexChanged(object sender, EventArgs e)
21
{
22
23
}
24
protected void myGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
25
{
26
myGrid.PageIndex = e.NewPageIndex;
27
BindGridView();
28
}
29
protected void myGrid_RowEditing(object sender, GridViewEditEventArgs e)
30
{
31
myGrid.EditIndex = e.NewEditIndex;
32
BindGridView();
33
}
34
protected void myGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
35
{
36
GridViewRow myGVR = myGrid.Rows[myGrid.EditIndex];
37
OleDbConnection myConn = new OleDbConnection(strConn);
38
string strSQL = "UPDATE STUREG SET LJ='"
39
+ ((TextBox)(myGrid.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',XM='"
40
+ ((TextBox)(myGrid.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' WHERE XH='"
41
+ myGrid.DataKeys[e.RowIndex].Value.ToString() + "'";
42
OleDbCommand myCMD;
43
try
44
{
45
myConn.Open();
46
myCMD = new OleDbCommand(strSQL, myConn);
47
myCMD.ExecuteNonQuery();
48
}
49
catch (OleDbException myOLEDBe)
50
{
51
Response.Write(myOLEDBe.Message.ToString());
52
}
53
finally
54
{
55
Response.Write("<br>" + strSQL);
56
myConn.Close();
57
myGrid.EditIndex = -1;
58
BindGridView();
59
}
60
}
61
protected void myGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
62
{
63
myGrid.EditIndex = -1;
64
BindGridView();
65
}
66
private void BindGridView()
67
{
68
OleDbConnection myConn = new OleDbConnection(strConn);
69
string strSQL = "SELECT * FROM STUREG ORDER BY LJ,XH";
70
OleDbDataAdapter myDA;
71
DataSet myDS = new DataSet();
72
try
73
{
74
myConn.Open();
75
myDA = new OleDbDataAdapter(strSQL, myConn);
76
myDA.Fill(myDS, "STU");
77
myGrid.DataSource = myDS.Tables["STU"];
78
myGrid.DataKeyNames = new string[] { "XH" };
79
myGrid.DataBind();
80
}
81
catch (OleDbException myOLEDBe)
82
{
83
Response.Write(myOLEDBe.Message.ToString());
84
}
85
finally
86
{
87
myConn.Close();
88
}
89
}
90
}
91

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

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91
