我猜你是想通過兩個不用的sub對一個listview進(jìn)行操作:
創(chuàng)新互聯(lián)建站專注于企業(yè)成都全網(wǎng)營銷推廣、網(wǎng)站重做改版、莊河網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、電子商務(wù)商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為莊河等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
如果你的第一次操作是放在sub form_load()中,因為vb.net只為每個窗體提供了一個form_load,就是窗體運行時自動加載的項。你的第二次操作不再放在form_load()中系統(tǒng)就沒辦法自動再加載你的第二次操作,要用事件觸發(fā)再能完成你的第二次操作.
1:讀取數(shù)據(jù)庫,前提你必須會讀取sqlserver數(shù)據(jù)庫,返回datatable
2:綁定到dgv上
3:導(dǎo)出到excel文件。
內(nèi)存 的釋放會由clr 自動釋放,你不用管這個。
這里是一個按鈕 導(dǎo)入Excel數(shù)據(jù)到 數(shù)據(jù)庫,刷新datagridview。
//導(dǎo)入Excel數(shù)據(jù)
private void tsBtnCopy_Click(object sender, EventArgs e)
{
DialogResult dresult = this.openFileDialog1.ShowDialog();
if (dresult == DialogResult.OK)
{
string filepath = openFileDialog1.FileName;
if (filepath == "")
{
MessageBox.Show("請選擇要導(dǎo)入的Excel文檔!", "系統(tǒng)提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
try
{
DataSet ds = new DataSet();
//取得數(shù)據(jù)集
//調(diào)用下面的函數(shù)
ds = xsldata(filepath);
//dataGridView2.DataSource = ds.Tables[0];
int errorcount = 0;//記錄錯誤信息條數(shù)
int updatecount = 0;//更新信息條數(shù)
int insertcount = 0;//記錄插入成功條數(shù)
string mess = "";
for (int i = 0; i ds.Tables[0].Rows.Count; i++)
{
StudentsModel stuModel = new StudentsModel();
stuModel.StuID = Guid.NewGuid().ToString();
stuModel.CenterID = CentersManager.manager.GetList("").Tables[0].Rows[0]["CenterID"].ToString();
stuModel.StuName = ds.Tables[0].Rows[i][0].ToString();
stuModel.Sex = ds.Tables[0].Rows[i][1].ToString();
stuModel.School = ds.Tables[0].Rows[i][2].ToString();
stuModel.Grade = ds.Tables[0].Rows[i][3].ToString();
stuModel.LinkWay = ds.Tables[0].Rows[i][4].ToString();
if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i][5].ToString()))
{
try
{
stuModel.BirthDay = Convert.ToDateTime(ds.Tables[0].Rows[i][5].ToString());
}
catch
{
mess+="第" + (i + 1) + "條數(shù)據(jù)生日格式不正確!\n";
}
}
stuModel.ParentName = ds.Tables[0].Rows[i][6].ToString();
stuModel.ParentLink = ds.Tables[0].Rows[i][7].ToString();
stuModel.Desc = ds.Tables[0].Rows[i][8].ToString();
if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i][9].ToString()))
{
try
{
stuModel.CreateTime = Convert.ToDateTime(ds.Tables[0].Rows[i][9].ToString());
}
catch
{
mess+="第" + (i + 1) + "條數(shù)據(jù)注冊時間格式不正確!\n";
}
}
stuModel.EndEditTime = DateTime.Now.Date;
DataTable stuTable = StudentsManager.manager.GetList(" StuName='" + stuModel.StuName + "' and ParentName='" + stuModel.ParentName + "' and ParentLink='"+stuModel.ParentLink+"' ").Tables[0];
if (stuTable.Rows.Count == 1)
{
string id = stuTable.Rows[0]["StuID"].ToString();
stuModel.StuID = id;
bool updateResult = StudentsManager.manager.Update(stuModel);
if (updateResult)
updatecount++;
else
errorcount++;
}
else
{
bool result = StudentsManager.manager.Add(stuModel);
if (result)
insertcount++;
else
errorcount++;
}
}
MessageBox.Show(insertcount + "條數(shù)據(jù)導(dǎo)入成功!"+updatecount+"條數(shù)據(jù)更新成功!\n\n" +mess+"出錯數(shù)據(jù)導(dǎo)入為空!") ;
Binddgv();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private DataSet xsldata(string filepath)
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;IMEX=1'";
System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
string strCom = "SELECT * FROM [Sheet1$]";
Conn.Open();
System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "[Sheet1$]");
Conn.Close();
return ds;
}
1、首先新建一個Windows應(yīng)用程序,將主窗體重命名為MainForm,在MainForm中加入一個DataGridView控件,命名為dgv_User。
2、打開窗體代碼窗口,在代碼窗口中聲明一個ComboBox的控件
// 定義下拉列表框
private ComboBox cmb_Temp = new ComboBox();
3、方法:
/// summary
/// 綁定下拉列表框
/// /summary
private void BindSex()
{
DataTable dtSex = new DataTable();
dtSex.Columns.Add("Value");
dtSex.Columns.Add("Name");
DataRow drSex;
drSex = dtSex.NewRow();
drSex[0] = "1";
drSex[1] = "";
dtSex.Rows.Add(drSex);
drSex = dtSex.NewRow();
drSex[0] = "0";
drSex[1] = "";
dtSex.Rows.Add(drSex);
cmb_Temp.ValueMember = "Value";
cmb_Temp.DisplayMember = "Name";
cmb_Temp.DataSource = dtSex;
cmb_Temp.DropDownStyle = ComboBoxStyle.DropDownList;
}
在配置文件app.config中加入
appSettings
add key="LotDBConnection" value="DATA SOURCE=數(shù)據(jù)庫連接;PASSWORD=密碼;PERSIST SECURITY INFO=True;USER ID=登錄名"/
/appSettings
//m_SqlPath 存放SQL語句的XML文件
Dim sqlHelper As New SQLHelper("LotDBConnection")
Dim sqlRead As New ResourceHelper
Dim ht As New Hashtable
Dim strSQL As String
Try
'設(shè)置SQL文參數(shù)
ht.Clear()
ht.Add(ColumnName.USER_ID.ToString, Trim(Me.txtUserID.Text))
ht.Add(ColumnName.USER_NAME.ToString, Trim(Me.txtUserName.Text))
'數(shù)據(jù)查詢
strSQL = sqlRead.GetSQLSentence("GetUserList", m_SqlPath, ht)
ds = sqlHelper.ExecuteDataSet(strSQL)
If ds.Tables(0).Rows.Count = 0 Then
MessageBox .Show ("數(shù)據(jù)不存在");
Else
'數(shù)據(jù)綁定
dgvUserMeisai.DataSource = ds.Tables(0)
End If
Catch ex As Exception
MessageBox .Show (ex.Message );
Finally
sqlHelper = Nothing
sqlRead = Nothing
ht = Nothing
End Try