1

私はC#データベース全体に不慣れです。SQL Server Management Studio 2012 でデータベースを作成し、Visual C# の DataGridView タスクから DataSource を選択して、(Windows フォームの) DataGrid に接続しました。私がやりたいことは、texboxes に新しいレコード データを入力してから、テキスト ボックス情報を DataGrid の新しい行に追加するボタンを押すことです。どんな助けでも大歓迎です。

4

3 に答える 3

0

これはおそらくあなたが望むものです。次のように、SQL Server データベースにテーブルを作成します。

CREATE TABLE [dbo].[StudentInfo](
    [StudentId] [numeric](2, 0) IDENTITY(1,1) NOT NULL,
    [SName] [varchar](35) NOT NULL,
    [FName] [varchar](35) NOT NULL,
    [Class] [varchar](35) NOT NULL,
 CONSTRAINT [PK_StudentInfo] PRIMARY KEY CLUSTERED 
(
    [StudentId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

Windowsアプリケーションでこのコーディングを使用して、作成されたテーブルのデータを削除および保存します

using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.DataGrid.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.DataGrid_CellClick);
        }
        SqlConnection cn = new SqlConnection("data source=localhost;initial catalog=acc;integrated security=true");
        private void Form1_Load(object sender, EventArgs e)
        {
            TBStudentID.Enabled = false;
            GetData();
        }
        private void GetData()
        {
            SqlDataAdapter da = new SqlDataAdapter("select * from StudentInfo", cn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            DataGrid.DataSource = dt;
        }
        private void BtnSave_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("insert into StudentInfo(SName, FName, Class) values('" + TBSName.Text + "','" + TBFName.Text + "','" + TBClass.Text + "')", cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            GetData();
        }
        // using this CellClick event you will be able to get the selected row values
        // in Respective TextBoxes by clicking any of the datagridview rows
        private void DataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                TBStudentID.Text = DataGrid.Rows[e.RowIndex].Cells[0].Value.ToString();
                TBSName.Text = DataGrid.Rows[e.RowIndex].Cells[1].Value.ToString();
                TBFName.Text = DataGrid.Rows[e.RowIndex].Cells[2].Value.ToString();
                TBClass.Text = DataGrid.Rows[e.RowIndex].Cells[3].Value.ToString();
            }
            catch (Exception)
            {
            }
        }
        private void TextBoxClear()
        {
            TBStudentID.Text = null;
            TBSName.Text = null;
            TBFName.Text = null;
            TBClass.Text = null;
        }
        private void BtnDelete_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("delete from StudentInfo where StudentId = '" + TBStudentID.Text + "'", cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            GetData();
            TextBoxClear();
        }

        private void BtnUpdate_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("update StudentInfo set SName = '" + TBSName.Text + "', FName = '" + TBFName.Text + "', Class = '" + TBClass.Text + "' where StudentId = '" + TBStudentID.Text + "'", cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            GetData();
            TextBoxClear();
        }
    }
}
于 2013-03-14T19:24:33.490 に答える
0

これを行うには、グリッド ビューの onrowdatabound メソッドを templatefield と組み合わせて使用​​します。

<asp:GridView ID="gridview" OnRowDataBound="grd_OnRowDataBound">
   <Columns>
      <asp:TemplateField HeaderText="#">
         <ItemTemplate>
            <asp:Label ID="id" runat="server" Text='<%# Bind("data value") %>'></asp:Label>
         </ItemTemplate>
      </asp:TemplateField>
   </Columns>
</asp:GridView>

次に、次のようにバックエンドでメソッドを作成します

 protected void grd_OnRowDataBound(Object sender, GridViewRowEventArgs e)
    {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
             TextBox txtbox = (TextBox)e.Row.FindControl("id");
         }
    }
于 2013-03-14T18:47:11.607 に答える
0

以下のコンセプトを試すことができます。いいえの私のメモを参照してください。3も。

public Form1()
{
    ds = new DataSet();
    // 1. Create connection.
    conn = new OleDbConnection(@"connection_string.");
    // 2. init SqlDataAdapter with select command and connection
    da = new OleDbDataAdapter("Select * from YouRtable", conn);

    // 3. fill in insert, update, and delete commands 

       (For No. 3 - You can search around the internet on how to construct a SQL Command). You can refer the values that you to insert from the textboxes that you have.)

    OleDbCommandBuilder cmdBldr = new OleDbCommandBuilder(da);
    da.Fill(ds, "YouRtable");
    dataGridView2.DataSource = ds;
    dataGridView2.DataMember = "YouRtable";
}

//Here is your save/update button code.
private void btnSaveGridData_Click(object sender, EventArgs e)
{
    da.Update(ds, "YouRtable");
}
于 2013-03-14T18:50:54.147 に答える