在不使用数据库的情况下将数据绑定到datagridview [英] Bind data to datagridview without using database
本文介绍了在不使用数据库的情况下将数据绑定到datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
1)我有3个下拉列表和1个datagridview,当我们在下拉列表中选择数据并单击添加按钮时,数据将添加到数据网格视图中.
2)现在我的问题是我只能将数据添加到数据网格视图中.因此,如何修改此代码以添加删除和重置按钮以删除数据网格视图中的选定行.
谢谢您的帮助.:)
hello ,
1)I have 3 dropdown list and 1 datagridview and when we select the data in dropdown list and click add button, the data will add into data grid view.
2)Now my problem is I only can add data into data grid view.So , how to modify this code to add delete and reset button to delete slected row in datagrid view.
thank you for help.:)
protected void BindGrid(int rowcount)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("Document No",
typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Document Title", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Revision", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("File", typeof(String)));
if (ViewState["CurrentData"] != null)
{
for (int i = 0; i < rowcount + 1; i++)
{
dt = (DataTable)ViewState["CurrentData"];
if (dt.Rows.Count > 0)
{
dr = dt.NewRow();
dr[0] = dt.Rows[0][0].ToString();
}
}
dr = dt.NewRow();
dr[0] = DropDownList19.Text;
dr[1] = DropDownList21.Text;
dr[2] = DropDownList20.Text;
dt.Rows.Add(dr);
}
else
{
dr = dt.NewRow();
dr[0] = DropDownList19.Text;
dr[1] = DropDownList21.Text;
dr[2] = DropDownList20.Text;
dt.Rows.Add(dr);
}
// If ViewState has a data then use the value as the DataSource
if (ViewState["CurrentData"] != null)
{
GridView1.DataSource = (DataTable)ViewState["CurrentData"];
GridView1.DataBind();
}
else
{
// Bind GridView with the initial data assocaited in the DataTable
GridView1.DataSource = dt;
GridView1.DataBind();
}
// Store the DataTable in ViewState to retain the values
ViewState["CurrentData"] = dt;
}
//////////////////////////add button//////////////////////////////////
protected void Button1_Click(object sender, EventArgs e)
{
// Check if the ViewState has a data assoiciated within it. If
if (ViewState["CurrentData"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentData"];
int count = dt.Rows.Count;
BindGrid(count);
}
else
{
BindGrid(1);
}
DropDownList4.Text = string.Empty;
DropDownList4.Focus();
{
string uploadedFilePath = @"C:\";
string sharePointListPath = "http://path/";
if (FileUpload1.HasFile)
try
{
FileUpload1.SaveAs(
uploadedFilePath + FileUpload1.FileName);
errorlabaelupload.Text = "File name: " +
FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + " bytes<br>" +
"Content type: " +
FileUpload1.PostedFile.ContentType;
UploadFileToSharePoint(
uploadedFilePath + FileUpload1.FileName,
sharePointListPath + FileUpload1.FileName);
}
catch (Exception ex)
{
errorlabaelupload.Text = "ERROR: " + ex.Message.ToString();
}
else
{
errorlabaelupload.Text = "You have not specified a file.";
}
}
}
}
}
推荐答案
看看我的文章是否可以帮忙...
删除DatagridView中的记录?
问候
Hi,
See my article if could help...
Delete record in DatagridView?
Regards,
这篇关于在不使用数据库的情况下将数据绑定到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文