在datagridviewcheckboxcolumn C#方面需要帮助 [英] Need help with datagridviewcheckboxcolumn C#

查看:72
本文介绍了在datagridviewcheckboxcolumn C#方面需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是C#编程的新手。我需要一些关于我项目的帮助。



我有datagridview我用我数据库中不同表的几个数据填充它,我的datagridview中的列是Choose(复选框)列),项目(文本框列)和详细信息(文本框列)。

我想要做的是,当用户选中选择列中的一行或更多行中的复选框它会将检查的行保存为我的新数据库中的新记录。



这里执行的代码的一小部分,

Hi, i'm still newbie in C# programming. I need some help on my project.

I have datagridview that i fill it with couple data from different table in my database, the column in my datagridview is Choose(checkbox column), Item(textbox column) and Detail(textbox column).
what i want to do is, when user checked the checkbox in a row or more in Choose column it will saved the checked row as new record in my new database.

here little part of my code to execute that,

DataTable dtChanged = ((DataTable)dgvItem.DataSource).GetChanges();
if (dtChanged == null)
    return;

SqlConnection sqlConn = new SqlConnection(GlobalVar.gstrConnStr);
sqlConn.Open();

SqlCommand sqlCmd = new SqlCommand();

foreach (DataRow drChanged in dtChanged.Rows)
{
    if (!(Boolean)drChanged["Choosen"])
        continue;

    if (drChanged["Choosen"].ToString() == "True")
    {
        string strSeq = drChanged["Choosen"].ToString();
        string strItemID = drChanged["Items"].ToString().Replace("'", "''");

        string strSql = string.Format("INSERT INTO mkar1(meID,meSeq,meItemID,meUser,meDate)" +
            "VALUES ('{0}',{1},'{2}','{3}','{4:yyyy/MM/dd HH:mm:ss}')",
            txtNIKmkar.Text.Trim().Replace("'", "''"), strSeq, strItemID, GlobalVar.gstrConnStr, DateTime.Now);

        sqlCmd.CommandText = strSql;
        sqlCmd.Connection = sqlConn;
        sqlCmd.ExecuteNonQuery();
    }
}
sqlCmd.Dispose();
sqlConn.Close();
sqlConn.Dispose();



i知道我写的代码仍有问题,我需要一些帮助来解决它,因为我不知道该怎么做。



和我将要使用的数据库,

meID(PK,nchar 15,not null)

meSeq(PK, tinyint,not null)

meItemID(nchar 10,not null)

meUser(nchar 20,not null)

meDate(datetime,not not null)



这是我的问题,

1.如何保存到数据库中?

2对于meSeq列,我必须为每个选中的行保存数字编号从0开始。如何将每个选中的行保存为新记录?



我需要做什么?



please帮助,我将非常感谢每一个答案。


i know the code i write is still have problem and i need some help to fix it because i dont know how to do it.

and the database i will going to use,
meID (PK, nchar 15, not null)
meSeq (PK, tinyint, not null)
meItemID (nchar 10, not null)
meUser (nchar 20, not null)
meDate (datetime, not null)

And here is my question,
1. how to save into the database?
2. as for the column "meSeq" i have to save numeric number start from 0 for every checked row. how can i save every checked row as new record?

what i need to do?

please help, i will greatly appreciate every answer.

推荐答案

1。要保存到数据库,您应该写如下:



1. To save to database you should write something like :

using (SqlConnection con = new SqlConnection(connectionString))
{
           
    //
    // Open the SqlConnection.
    //
    con.Open();

    string insertString = @"insert into yourdatabase (meID,meSeq, meItemID,meUser, meDate) values ('id','seq','itemId','user','date')";
    SqlCommand cmd = new SqlCommand(insertString, connectionString);
    cmd.ExecuteNonQuery();

    con.Close();

}





2.首先,现场MeSeq可能是FK。在这种情况下,您可以为用户保存每个选中的行或添加新的。



2. First of all, field MeSeq is probably a FK. In this case your can save every checked row for a user or add new.


感谢piyush_singh和Alexander Dymshyts愿意提供答案。

i'我解决了它,实际上它只需要新的变量来增加每个选中的复选框作为新记录添加。



thanks to piyush_singh and Alexander Dymshyts for being willing to provide answer.
i've solved it, actually it just need new variable for increment each checked checkbox to be added as new record.

int intSeq = -1;
foreach (DataRow drChanged in dtChanged.Rows)
            {
                if (!(Boolean)drChanged["Choosen"])
                    continue;
 
                if ((Boolean)drChanged["Choosen"])
                {
                    intSeq++;
                    string strItemID = drChanged["Items"].ToString().Replace("'", "''");
 
                    string strSql = string.Format("INSERT INTO  mkar1(meID,meSeq,meItemID,meUser,meDate)" +
                        "VALUES ('{0}',{1},'{2}','{3}','{4:yyyy/MM/dd HH:mm:ss}')",
                        txtNIKmkar.Text.Trim().Replace("'", "''"), intSeq, strItemID, GlobalVar.gstrUsrStr, DateTime.Now);


这篇关于在datagridviewcheckboxcolumn C#方面需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆