Laploy Web Board
Would you like to react to this message? Create an account in a few clicks or log in to continue.

พิมพ์ใน datagridview เพิ่มข้มูลลงดาต้าเบส

2 posters

Go down

พิมพ์ใน datagridview เพิ่มข้มูลลงดาต้าเบส Empty พิมพ์ใน datagridview เพิ่มข้มูลลงดาต้าเบส

ตั้งหัวข้อ  rungsak 13th May 2011, 4:24 pm

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.Data.SqlTypes;

namespace WindowsFormsApplication
{
public partial class frmHome : Form
{


public frmHome()
{
InitializeComponent();
}

private void frmHome_Load(object sender, EventArgs e)
{
BindDataGrid();
}

private void BindDataGrid()
{
SqlCeConnection myConnection = default(SqlCeConnection);
DataTable dt = new DataTable();
SqlCeDataAdapter Adapter = default(SqlCeDataAdapter);
//myConnection = new SqlCeConnection("Data Source =" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\AppDatabase1.sdf;"));
myConnection = new SqlCeConnection("Data Source =C:\\WindowsFormsApplication\\WindowsFormsApplication\\Database1.sdf;");
myConnection.Open();
SqlCeCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "SELECT [id], [name], [email] FROM [mytable]";
myCommand.CommandType = CommandType.Text;

Adapter = new SqlCeDataAdapter(myCommand);
Adapter.Fill(dt);

myConnection.Close();

this.dgName.DataSource = dt;

this.dgName.Columns.Clear();

DataGridViewTextBoxColumn column;

column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "id";
column.HeaderText = "ID";
column.Width = 50;
this.dgName.Columns.Add(column);

column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "name";
column.HeaderText = "Name";
column.Width = 100;
this.dgName.Columns.Add(column);

column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "email";
column.HeaderText = "Email";
column.Width = 150;
this.dgName.Columns.Add(column);

dt = null;
}

private void Add_Click(object sender, EventArgs e)
{
// ต่อจากตรงนี้ไม่รู้คับว่าจะเขีนยังไง ผมสร้างหน้า form มา หนึ่งหน้า แล้วมี ดาต้ากริด กับปุ๋มเพิ่มข้อมูลคับ
}
}
}



rungsak

จำนวนข้อความ : 3
Join date : 05/02/2011

ขึ้นไปข้างบน Go down

พิมพ์ใน datagridview เพิ่มข้มูลลงดาต้าเบส Empty ง่ายกว่า

ตั้งหัวข้อ  Admin 13th May 2011, 4:38 pm

ทำแบบนี้จะง่ายกว่านะครับ
http://www.codeproject.com/Answers/171736/Submit-changes-from-DataGridView-which-its-datasou.aspx#answer1

Admin
Admin

จำนวนข้อความ : 215
Join date : 17/12/2009

http://laploy.forumotion.com

ขึ้นไปข้างบน Go down

ขึ้นไปข้างบน

- Similar topics

 
Permissions in this forum:
คุณไม่สามารถพิมพ์ตอบ