Click here to Skip to main content
15,891,788 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
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.:)

C#
 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.";
                }
            }


        }

    }
}
Posted
Updated 13-Nov-12 9:49am
v2
Comments
Christian Graus 13-Nov-12 11:09am    
This is just a code dump, not a clear focused question.
xalanxic 13-Nov-12 11:12am    
actually i want to delete row that was add into datagrid view.selected row will deleted when i click delete button.
Christian Graus 13-Nov-12 11:16am    
http://www.codeproject.com/Questions/291582/adding-a-col-which-contains-a-delete-button-for-ea - found this with google, but the site lets you search previous answers, too
xalanxic 13-Nov-12 11:29am    
thanks ...

1 solution

Hi,

See my article if could help...

Delete record in DatagridView?

Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900