|
|
|
Update datagridview ต้องการให้ datagridview แสดงข้อมูลเวลาเพิ่มข้อมูลลงฐานข้อมูล |
|
|
|
|
|
|
|
ต้องการให้ datagridview แสดงข้อมูลเวลาเพิ่มข้อมูลลงฐานข้อมูล
จากโค้สามารถแสดงได้แค่ครั้งเดียว คือเวลาเพิ่มครั้งแรก datagridview มีการีอัพเดตข้อมูล แต่เมื่อเพิ่มข้อมูลครั้งที่สอง datagridview ไม่อัพเดตให้คะ
จะต้องเขียนโค้ดยังไงให้มันอัพเดตได้เวลาเพิ่มข้อมูลครั้งต่อๆไป
และมีปัญหาตรงโค้ดเวลาเช็คว่าข้อมูลที่เพิ่มไปนั้นซ้ำหรือไม่จะ error ตรงที่
int c = ds.Tables["Program"].Rows.Count - 1;
ขอความช่วยเหลือด้วยนะคะ
Code (C#)
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace LastProject
{
public partial class ManageUser : Form
{
public ManageUser()
{
InitializeComponent();
}
SqlConnection Conn;
string strConn;
DataSet ds = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'server1DataSet.Program' table. You can move, or remove it, as needed.
this.programTableAdapter.Fill(this.server1DataSet.Program);
strConn = "Data Source=SKZ-43D05CDB745;Initial Catalog=server1;Integrated Security=True; User ID=sa;Password=1234";
Conn = new SqlConnection();
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
}
private void check()
{
int c = ds.Tables["Program"].Rows.Count - 1;
for (int i = 0; i <= c; i++)
{
if (ds.Tables["Program"].Rows[i]["NameProgram"].ToString().Equals(txtNameProgram.Text.Trim()))
{
MessageBox.Show("คุณป้อนข้อมูลซ้ำ !!! กรุณาป้อนข้อมูลใหม่", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
txtID.Text = "";
txtID.Focus();
txtNameProgram.Text = "";
txtNameProcess.Text = "";
return;
}
}
}
private void ShowData() //Refresh Data Gridview and ListDelete Program
{
/*
StringBuilder sb = new StringBuilder();
sb.Remove(0, sb.Length);
sb.Append("select IDProgram,NameProgram,NameProcess");
sb.Append("from Program");
sb.Append("Order by IDProgram");
string sqldata = sb.ToString();
DataTable dt;
SqlCommand com = new SqlCommand();
com.CommandText = sqldata;
com.CommandType = CommandType.Text;
com.Connection = Conn;
SqlDataReader dr;
dr = com.ExecuteReader();
if (dr.HasRows)
{
dt = new DataTable();
dt.Load(dr);
dgvProgram.DataSource = dt;
}
else
{
dgvProgram.DataSource = null;
}
dr.Close();
*/
string sqldata = " select * from Program ";
SqlDataAdapter da = new SqlDataAdapter(sqldata, Conn);
da.Fill(ds, "Program");
dgvProgram.ReadOnly = true;
// da.Update(ds, "Program");
dgvProgram.DataSource = ds.Tables["Program"];
string sqldata1 = "select NameProgram from Program";
SqlDataAdapter da1 = new SqlDataAdapter(sqldata1, Conn);
da1.Fill(ds, "Data2");
checkedListBox.DataSource = ds.Tables["Data2"];
checkedListBox.DisplayMember = "NameProgram";
checkedListBox.ValueMember = "NameProgram";
checkedListBox.SelectedIndex = 0;
}
private void Add_Click(object sender, EventArgs e)
{
if (txtID.Text.Trim() == "" || txtNameProgram.Text.Trim() == "" || txtNameProcess.Text.Trim() == "")
{
MessageBox.Show("กรุณากรอกข้อมูลให้ครบ !!!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
txtID.Text = "";
txtID.Focus();
txtNameProgram.Text = "";
txtNameProcess.Text = "";
return;
}
if (MessageBox.Show("คุณต้องการเพิ่มโปรแกรมใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
{
// check();
strConn = "Data Source=SKZ-43D05CDB745;Initial Catalog=server1;Integrated Security=True; User ID=sa;Password=1234";
Conn = new SqlConnection();
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
StringBuilder sb = new StringBuilder();
SqlTransaction tr = Conn.BeginTransaction();
try
{
sb.Remove(0, sb.Length);
sb.Append("INSERT INTO Program (IDProgram,NameProgram,NameProcess)");
sb.Append("VALUES(@IDProgram,@NameProgram, @NameProcess)");
string sqlAdd;
sqlAdd = sb.ToString();
SqlCommand com = new SqlCommand();
com.CommandText = sqlAdd;
com.CommandType = CommandType.Text;
com.Connection = Conn;
com.Transaction = tr;
com.Parameters.Clear();
com.Parameters.Add("@IDProgram", SqlDbType.Int).Value = txtID.Text.Trim(); ;
com.Parameters.Add("@NameProgram", SqlDbType.NVarChar).Value = txtNameProgram.Text.Trim();
com.Parameters.Add("@NameProcess", SqlDbType.NVarChar).Value = txtNameProcess.Text.Trim();
com.ExecuteNonQuery();
tr.Commit();
MessageBox.Show("เพิ่มข้อมูลโปรแกรมเรียบร้อยแล้ว!!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtID.Text = "";
txtID.Focus();
txtNameProgram.Text = "";
txtNameProcess.Text = "";
ShowData();
}
catch (Exception ex)
{
MessageBox.Show("ไม่สามารถเพิ่มข้อมูลโปรแกรมได้ เนื่องจาก " + ex.Message, "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
txtID.Text = "";
txtID.Focus();
txtNameProgram.Text = "";
txtNameProcess.Text = "";
}
}
}
Tag : - - - -
|
|
|
|
|
|
Date :
2009-05-09 16:10:35 |
By :
datagridview |
View :
3999 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ผมไม่รู้จะดูให้ตรงไหนครับ เอาเป็นว่าลองดู Code ที่ผมเขียนขึ้นมาน่ะครับ
Code
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
OleDbConnection objConn;
OleDbCommand objCmd;
String strSQL;
void Page_Load(object sender,EventArgs e)
{
String strConnString;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("database/mydatabase.mdb") + ";";
objConn = new OleDbConnection(strConnString);
objConn.Open();
if(!Page.IsPostBack)
{
BindData();
}
}
void BindData()
{
String strSQL;
strSQL = "SELECT * FROM customer";
OleDbDataReader dtReader;
objCmd = new OleDbCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
//*** BindData to GridView ***//
myGridView.DataSource = dtReader;
myGridView.DataBind();
dtReader.Close();
dtReader = null;
}
void Page_UnLoad()
{
objConn.Close();
objConn = null;
}
void modEditCommand(Object sender, GridViewEditEventArgs e)
{
myGridView.EditIndex = e.NewEditIndex;
myGridView.ShowFooter = false;
BindData();
}
void modCancelCommand(Object sender, GridViewCancelEditEventArgs e)
{
myGridView.EditIndex = -1;
myGridView.ShowFooter = true;
BindData();
}
void modDeleteCommand(Object sender, GridViewDeleteEventArgs e)
{
strSQL = "DELETE FROM customer WHERE CustomerID = '" + myGridView.DataKeys[e.RowIndex].Value + "'";
objCmd = new OleDbCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
myGridView.EditIndex = -1;
BindData();
}
void myGridView_RowCommand(Object source , GridViewCommandEventArgs e)
{
if (e.CommandName == "Add")
{
//*** CustomerID ***//
TextBox txtCustomerID = (TextBox)myGridView.FooterRow.FindControl("txtAddCustomerID");
//*** Email ***//
TextBox txtName = (TextBox)myGridView.FooterRow.FindControl("txtAddName");
//*** Name ***//
TextBox txtEmail = (TextBox)myGridView.FooterRow.FindControl("txtAddEmail");
//*** CountryCode ***//
TextBox txtCountryCode = (TextBox)myGridView.FooterRow.FindControl("txtAddCountryCode");
//*** Budget ***//
TextBox txtBudget = (TextBox)myGridView.FooterRow.FindControl("txtAddBudget");
//*** Used ***//
TextBox txtUsed = (TextBox)myGridView.FooterRow.FindControl("txtAddUsed");
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
" VALUES ('" + txtCustomerID.Text + "','" + txtName.Text + "','" + txtEmail.Text + "' " +
" ,'" + txtCountryCode.Text + "','" + txtBudget.Text + "','" + txtUsed.Text + "') ";
objCmd = new OleDbCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
BindData();
}
}
void modUpdateCommand(Object sender,GridViewUpdateEventArgs e)
{
//*** CustomerID ***//
TextBox txtCustomerID = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditCustomerID");
//*** Email ***//
TextBox txtName = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditName");
//*** Name ***//
TextBox txtEmail = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditEmail");
//*** CountryCode ***//
TextBox txtCountryCode = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditCountryCode");
//*** Budget ***//
TextBox txtBudget = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditBudget");
//*** Used ***//
TextBox txtUsed = (TextBox)myGridView.Rows[e.RowIndex].FindControl("txtEditUsed");
strSQL = "UPDATE customer SET CustomerID = '" + txtCustomerID.Text + "' " +
" ,Name = '" + txtName.Text + "' " +
" ,Email = '" + txtEmail.Text + "' " +
" ,CountryCode = '" + txtCountryCode.Text + "' " +
" ,Budget = '" + txtBudget.Text + "' " +
" ,Used = '" + txtUsed.Text + "' " +
" WHERE CustomerID = '" + myGridView.DataKeys[e.RowIndex].Value + "'";
objCmd = new OleDbCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
myGridView.EditIndex = -1;
myGridView.ShowFooter = true;
BindData();
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - GridView</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView id="myGridView" runat="server" AutoGenerateColumns="False"
ShowFooter="True"
DataKeyNames="CustomerID"
OnRowEditing="modEditCommand"
OnRowCancelingEdit="modCancelCommand"
OnRowDeleting="modDeleteCommand"
OnRowUpdating="modUpdateCommand"
OnRowCommand="myGridView_RowCommand">
<Columns>
<asp:TemplateField HeaderText="CustomerID">
<ItemTemplate>
<asp:Label id="lblCustomerID" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CustomerID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditCustomerID" size="5" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CustomerID") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddCustomerID" size="5" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label id="lblName" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditName" size="10" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddName" size="10" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label id="lblEmail" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditEmail" size="20" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddEmail" size="20" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CountryCode">
<ItemTemplate>
<asp:Label id="lblCountryCode" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CountryCode") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditCountryCode" size="2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CountryCode") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddCountryCode" size="2" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Budget">
<ItemTemplate>
<asp:Label id="lblBudget" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Budget") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditBudget" size="6" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Budget") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddBudget" size="6" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Used">
<ItemTemplate>
<asp:Label id="lblUsed" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Used") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditUsed" size="6" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Used") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddUsed" size="6" runat="server"></asp:TextBox>
<asp:Button id="btnAdd" runat="server" Text="Add" CommandName="Add"></asp:Button>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" CancelText="Cancel" DeleteText="Delete" EditText="Edit" UpdateText="Update" HeaderText="Modify" />
<asp:CommandField ShowDeleteButton="True" HeaderText="Delete" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
Ref : (C#) ASP.NET GridView Control - RowCommand
|
|
|
|
|
Date :
2009-05-09 23:15:59 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณคะจาลองดูนะคะ
|
|
|
|
|
Date :
2009-05-10 11:49:02 |
By :
datagridview |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
myGridView.DataBind();
ถ้าใช้ MS SQL ตรง .DataBind(); แก้เป็นอาไรคะ ลองแก้เป็นอย่างอื่นแล้วแต่มัน error คะ ขอบคุณล่วงหน้าคะ
|
|
|
|
|
Date :
2009-05-10 12:08:04 |
By :
datagridview |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|