如何使用csharp在datagridview中显示保存记录 [英] how to display the save record in datagridview using csharp

查看:113
本文介绍了如何使用csharp在datagridview中显示保存记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private bool SaveDetails()

{

//文本框的验证不是空的

bool检查;

Check = true;

if(txt_Faccode.Text.Trim()。Length == 0)

{

Check = false;

MessageBox.Show(请输入学院代码,不输入详细信息,MessageBoxButtons.OK,MessageBoxIcon.Information);

返回检查;

}



if(Txt_Facname.Text.Trim()。Length == 0)

{

Check = false;

MessageBox.Show(请输入教员姓名,不输入详细信息,MessageBoxButtons.OK,MessageBoxIcon.Information);

返回检查;

}



if(txt_Hrs.Text.Trim()。Length == 0)

{

Check = false;

MessageBox.Show(请输入分配的小时数,不输入详细信息,MessageBoxButtons.OK,MessageBoxIcon.Information);

返回检查;

}





//插入更新数据库的代码

this.Cursor = Cursors.WaitCursor;

尝试

{

sql =插入Tb_SCH_Faculty_Details([Faculty_Code],[Faculty_Name], [Allocated_Hours])+值(''+'txt_Faccode.Text +'',''+ Txt_Facname.Text +'',+ txt_Hrs.Text +);



int temp = 0;

if(!int.TryParse(txt_Hrs.Text.Trim(),out temp))

{

Check = false;

this.Cursor = Cursors.Arrow;

MessageBox.Show(输入数字只在分配的小时,字符不允许,MessageBoxButtons.OK,MessageBoxIcon.Error);

返回检查;

}



GFun.Error =;

GFun.InsertAccessData(sql);

if(GFun.Error.ToString()!= )

{

Check = false;

MessageBox.Show(GFun.Error.ToString(),Error);

this.Cursor = Cursors.Arrow;

返回支票;

}



GFun .OleDbCon.Close();

}

catch(exception ex)

{

Check = false;

MessageBox.Show(ex.ToString(),Error,MessageBoxButtons.OK,MessageBoxIcon.Error);

this.Cursor = Cu rsors.Arrow;

返回支票;

}

this.Cursor = Cursors.Arrow;

返回支票;

}

private void Btn_Save_Click(object sender,EventArgs e)

{

if(SaveDetails() == true)

MessageBox.Show(记录插入成功,记录已插入,MessageBoxButtons.OK,MessageBoxIcon.Information);

}



运行模式如下;



教师代码文本框

教师姓名文本框

Alloated hrs textbox



当我输入所有细节并点击保存按钮时,上述三项记录应显示在datagridview中。



我怎么能用csharp。

解决方案

嗨我认为它会帮助你..



注意: - 这是使用MySql Databa如果有Sql Server那么只需用适当的NameSpace替换MySql(使用System.Data.SqlClient;)



  //  创建连接字符串 
con.ConnectionString = ConfigurationManager.ConnectionStrings [ ConString]。ConnectionString;
// 打开连接
con.Open();
// make mysql命令方法的对象
MySqlCommand cmd = new MySqlCommand( 从Tb_SCH_Faculty_Details中选择Faculty_Code,Faculty_Name,Allocated_Hours,con);
// MySql数据适配器的对象
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
// 数据表对象
DataTable dt = new DataTable();
// 存储数据
da.Fill(dt);
// 最后在网格视图控件中设置值
datagridview.DataSource = DT;
datagridview.DataBind();
// 关闭连接
con.Close();


private bool SaveDetails()
{
//validation for textbox not empty
bool Check;
Check = true;
if (txt_Faccode.Text.Trim().Length == 0)
{
Check = false;
MessageBox.Show("Please enter the Faculty code","Not Enter the details",MessageBoxButtons.OK,MessageBoxIcon.Information);
return Check;
}

if (Txt_Facname.Text.Trim().Length == 0)
{
Check = false;
MessageBox.Show("Please enter the Faculty Name","Not Enter the details",MessageBoxButtons.OK,MessageBoxIcon.Information);
return Check;
}

if (txt_Hrs.Text.Trim().Length == 0)
{
Check = false;
MessageBox.Show("Please enter the allocated hours","Not Enter the details",MessageBoxButtons.OK,MessageBoxIcon.Information);
return Check;
}


//insert code for update to the DataBase
this.Cursor = Cursors.WaitCursor;
try
{
sql = "insert into Tb_SCH_Faculty_Details ([Faculty_Code], [Faculty_Name],[Allocated_Hours]) " + " values(''" + txt_Faccode.Text + "'',''" + Txt_Facname.Text + "'', " + txt_Hrs.Text + ")";

int temp = 0;
if (!int.TryParse(txt_Hrs.Text.Trim(), out temp))
{
Check = false;
this.Cursor = Cursors.Arrow;
MessageBox.Show("Enter Numbers only in allocated hours", "Characters Not Allowed", MessageBoxButtons.OK, MessageBoxIcon.Error);
return Check;
}

GFun.Error = "";
GFun.InsertAccessData(sql);
if (GFun.Error.ToString() != "")
{
Check = false;
MessageBox.Show(GFun.Error.ToString(), "Error");
this.Cursor = Cursors.Arrow;
return Check;
}

GFun.OleDbCon.Close();
}
catch (Exception ex)
{
Check = false;
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Cursor = Cursors.Arrow;
return Check;
}
this.Cursor = Cursors.Arrow;
return Check;
}
private void Btn_Save_Click(object sender, EventArgs e)
{
if (SaveDetails() == true)
MessageBox.Show("Record Inserted Successfully", "Records Inserted", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

run mode as follows;

Faculty code textbox
Faculty name textbox
Alloated hrs textbox

when i enter all the details and click the save button, the above three items records should be displayed in datagridview.

for that how can i do using csharp.

解决方案

Hi i think it will helps you..

note:- this is using MySql Database if there is Sql Server then just replace MySql with Sql using proper NameSpace(Using System.Data.SqlClient;)

//make a connection string
 con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
// Opent the connection
            con.Open();
// make object of Mysql command method
            MySqlCommand cmd = new MySqlCommand("select Faculty_Code,Faculty_Name,Allocated_Hours from Tb_SCH_Faculty_Details", con);
// object of MySql Data Adapter
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
// object of data table
            DataTable dt = new DataTable();
// store the data
            da.Fill(dt);
//finally set the value in grid view control
            datagridview.DataSource = dt;
            datagridview.DataBind();
// close the connection
            con.Close();


这篇关于如何使用csharp在datagridview中显示保存记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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