none
bind list attachments to grid and delte it RRS feed

Answers

  • SPWeb web = SPContext.Current.Web;
                    SPList list = web.Lists["sample"];
                    int currListItemId = SPContext.Current.ItemId;
                    SPListItem currentListItem = list.GetItemById(currListItemId);

                    int nRowValue = 0;
                    string[] strFiles;
                    string[] strFilenames = new string[100];
                    strFiles = new String[currentListItem.Attachments.Count];

                    foreach (string strAttachment in currentListItem.Attachments)
                    {
                        strFiles[nRowValue] = currentListItem.Attachments.UrlPrefix + strAttachment;
                        strFilenames[nRowValue] = strAttachment;
                        nRowValue += 1;
                    }
                    DataTable dt = new DataTable();
                    dt.Columns.Add("FilePath");
                    dt.Columns.Add("FileName");
                    dt.Columns.Add("FileNameToUser");
                    for (int count = 0; count < strFiles.Length; count++)
                    {
                        dt.Rows.Add();
                        int j = Convert.ToString(strFilenames[count]).LastIndexOf('_');
                        dt.Rows[count]["FilePath"] = strFiles[count].ToString();
                        dt.Rows[count]["FileName"] = Convert.ToString(strFilenames[count]);
                        if (j > -1)
                        {
                            dt.Rows[count]["FileNameToUser"] = Convert.ToString(strFilenames[count]).Substring(j).TrimEnd('_');
                        }
                        else
                        {
                            dt.Rows[count]["FileNameToUser"] = Convert.ToString(strFilenames[count]).ToString();
                        }

                    }
                    dgdUpload.DataSource = dt;
                    dgdUpload.DataBind();

     


     string apprName = dgdUpload.DataKeys[e.RowIndex].Value.ToString();
                   
                    SPWeb web = SPContext.Current.Web;
                    web.AllowUnsafeUpdates = true;
                    SPList whpdataList = web.Lists["sample"];
                    int currListItemId = SPContext.Current.ItemId;
                    SPListItem currentListItem = whpdataList.GetItemById(currListItemId);               
                    int itmcount = currentListItem.Attachments.Count;
                   
                    if (itmcount > 0)
                    {                   
                        foreach (string filename in currentListItem.Attachments)
                        {
                            SPFile fil = currentListItem.ParentList.ParentWeb.GetFile(currentListItem.Attachments.UrlPrefix + filename);
                            if (fil.Name.ToLower() == apprName.ToLower())
                            {
                                fil.Delete();
                                break;
                            }                       
                        }
                    }
                    //dgdUpload.DataSource = null;
                    //dgdUpload.DataBind();
                    //string[] strFiles;
                    //strFiles = new String[currentListItem.Attachments.Count];
                    //string[] strFilenames = new string[100];
                    //FetchGrid(currentListItem, strFiles, strFilenames);
                    bindGridViewAttachments();

                    web.AllowUnsafeUpdates = false;

    Friday, July 6, 2012 7:25 AM

All replies

  • SPWeb web = SPContext.Current.Web;
                    SPList list = web.Lists["sample"];
                    int currListItemId = SPContext.Current.ItemId;
                    SPListItem currentListItem = list.GetItemById(currListItemId);

                    int nRowValue = 0;
                    string[] strFiles;
                    string[] strFilenames = new string[100];
                    strFiles = new String[currentListItem.Attachments.Count];

                    foreach (string strAttachment in currentListItem.Attachments)
                    {
                        strFiles[nRowValue] = currentListItem.Attachments.UrlPrefix + strAttachment;
                        strFilenames[nRowValue] = strAttachment;
                        nRowValue += 1;
                    }
                    DataTable dt = new DataTable();
                    dt.Columns.Add("FilePath");
                    dt.Columns.Add("FileName");
                    dt.Columns.Add("FileNameToUser");
                    for (int count = 0; count < strFiles.Length; count++)
                    {
                        dt.Rows.Add();
                        int j = Convert.ToString(strFilenames[count]).LastIndexOf('_');
                        dt.Rows[count]["FilePath"] = strFiles[count].ToString();
                        dt.Rows[count]["FileName"] = Convert.ToString(strFilenames[count]);
                        if (j > -1)
                        {
                            dt.Rows[count]["FileNameToUser"] = Convert.ToString(strFilenames[count]).Substring(j).TrimEnd('_');
                        }
                        else
                        {
                            dt.Rows[count]["FileNameToUser"] = Convert.ToString(strFilenames[count]).ToString();
                        }

                    }
                    dgdUpload.DataSource = dt;
                    dgdUpload.DataBind();

     


     string apprName = dgdUpload.DataKeys[e.RowIndex].Value.ToString();
                   
                    SPWeb web = SPContext.Current.Web;
                    web.AllowUnsafeUpdates = true;
                    SPList whpdataList = web.Lists["sample"];
                    int currListItemId = SPContext.Current.ItemId;
                    SPListItem currentListItem = whpdataList.GetItemById(currListItemId);               
                    int itmcount = currentListItem.Attachments.Count;
                   
                    if (itmcount > 0)
                    {                   
                        foreach (string filename in currentListItem.Attachments)
                        {
                            SPFile fil = currentListItem.ParentList.ParentWeb.GetFile(currentListItem.Attachments.UrlPrefix + filename);
                            if (fil.Name.ToLower() == apprName.ToLower())
                            {
                                fil.Delete();
                                break;
                            }                       
                        }
                    }
                    //dgdUpload.DataSource = null;
                    //dgdUpload.DataBind();
                    //string[] strFiles;
                    //strFiles = new String[currentListItem.Attachments.Count];
                    //string[] strFilenames = new string[100];
                    //FetchGrid(currentListItem, strFiles, strFilenames);
                    bindGridViewAttachments();

                    web.AllowUnsafeUpdates = false;

    Friday, July 6, 2012 7:25 AM
  • Hi Manju,

    Welcome to the MSDN Forum.

    What question do you want to ask?

    Where does the code fail?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 9, 2012 8:43 AM
    Moderator